【发布时间】:2010-02-12 11:31:31
【问题描述】:
首先,我的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 的东西来创建测试数据?
【问题讨论】:
标签: django unit-testing django-models