断言 post 有效的 Django

assert post worked 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

谢谢!

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

我在我的 post 参数中传递了一个 "organization",而表单需要一个整数(组织的 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())