【发布时间】:2017-06-01 06:23:38
【问题描述】:
我的视图代码如下所示:
if self.request.POST.get('is_useful'):
help_obj = get_object_or_404(Help, pk=self.kwargs['pk'], answer_to__author__username=self.request.POST.get('sender'))
help_obj.useful = True
print(help_obj)
help_obj.save()
print(help_obj)
response = json.dumps({'message': 'marked as useful help!'})
我正在使用它测试:
def test_logged_user_can_mark_help_as_useful(self):
self.client.login(username='bunny', password='pass')
response = self.client.post(reverse('questions:edit-help', kwargs={'pk': 1}), {'is_useful': True, 'sender': 'bunny'})
content = json.loads(response.content)
print(self.help1)
self.assertTrue('useful' in content['message'])
self.assertTrue(self.help1.useful)
问题是self.assertTrue(self.help1.useful)不通过,不管视图中的help_obj.useful = True。每个打印操作都显示帮助对象的 ID 为 1,并且它是唯一用于测试的对象。
第一个断言通过了。
任何帮助将不胜感激。谢谢!
【问题讨论】:
标签: python django unit-testing testing