首页 > 解决方案 > 断言帖子工作 Django

问题描述

我刚刚写了一些关于我的表格的测试用例,这是一个:

def test_department_admin_creation(self):

    nb = Department.objects.count()
    response = self.client.post(self.url, {"name" : 'department', "organization" : self.organization})
    self.assertEqual(response.status_code, 200)
    self.assertEqual(nb+1,Department.objects.count())

而且我想知道为什么最后一个断言不起作用,而 status_code 断言起作用。

 AssertionError: 2 != 1

谢谢 !

标签: djangotestingpostassertion

解决方案


感谢 Daniel Roseman,我找到了解决方案:

我在我的帖子参数中传递了一个“组织”,而表单需要一个整数(组织的 ID)。正确的代码是:

    nb = Department.objects.count()
    response = self.client.post(self.url, {"name" : 'department', "organization" : self.organization.id})
    self.assertEqual(response.status_code, 302)
    self.assertEqual(nb+1,Department.objects.count())

推荐阅读