【问题标题】:assert post worked Django断言帖子工作 Django
【发布时间】:2018-05-03 08:23:32
【问题描述】:

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

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

谢谢!

【问题讨论】:

  • 我不知道什么是status_code,但是那个测试清楚的告诉你提交没有成功,部门没有创建;您大概有 200,因为重新显示了无效的表单。通常一个成功的 POST 会重定向到一个新页面,即 302。
  • 哦,是的,你是对的,我修复了它,将“self.organization”更改为“self.organization.id”,而且我显然也更改了预期的状态码......谢谢!

标签: django testing post assertion


【解决方案1】:

感谢 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())

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-09
    • 1970-01-01
    • 2016-07-18
    • 1970-01-01
    • 2013-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多