首先,我的tests.py的代码
def test_get_current(self):
m = Member.objects.create(...)
q = Question.objects.create(name="q1", text="q1", start_datetime=self.day_before, close_datetime=self.day_after, type=self.type)
r = Response.objects.create(question=q, text='response')
expected = q, None
#self.assertEquals(expected, Question.objects.get_current(m.id))
q2 = Question.objects.create(name="q2", text="q2", start_datetime=self.day_before, close_datetime=self.day_after, type=self.type)
#print Question.objects.all()
#self.assertEquals(expected, Question.objects.get_current(m.id))
MemberResponse.objects.create(member=m, response=r)
print Question.objects.all().exclude(response__memberresponse__member=m)
print Question.objects.all().exclude(response__memberresponse__member=m)
我在 get_current 函数中得到了意想不到的结果,因此,我对其进行了评论并尝试通过打印函数内部使用的主查询集进行调试,并且还得到了奇怪的结果:
...
Installing index for ... model
[<Question: q1>, <Question: q2>]
[<Question: q2>]
.....
----------------------------------------------------------------------
Ran 5 tests in 3.125s
我想知道,为什么具有相同参数的 QuerySet 首先返回错误的数据,但在下一次调用时返回正确的数据,如何避免它?
顺便问一下,django world 有没有类似于 Rail 的 Factory Girl 的东西来创建测试数据?
最佳答案
factory_boy是“基于thoughtbot的factory_girl的固定装置替换。”
如果您来自 Rails,您会发现它的工作方式与factory_girl 非常相似。推荐。
关于Django 模型缓存?如何在测试中禁用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2251434/