django - 断言帖子工作 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,我找到了解决方案:
我在我的帖子参数中传递了一个“组织”,而表单需要一个整数(组织的 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())
推荐阅读
- parsing - 我将如何在我的语法中实现运算符优先级?
- sirikit - .continueInApp 后我还能让 Siri 说话吗?
- python - 树数据结构__str__方法
- reactjs - 根据 ReactJS 中的身份验证将用户重定向到不同路由的正确方法
- python - 从文件读取时计算一行中有多少'/'
- firebase - Firebase 存储:file().move 未实现?
- java - 在 JMonkeyEngine 中,如何在场景前拥有 FPS 风格的武器
- android - 如果某个字段等于某个值,有没有办法检查 Firestore 文档?
- android - 为什么 viewModel() 在 Activity 或 Fragment 中用于 Composabe 和 viewModels() ?
- typescript - 添加泛型方法会引发类型错误:对象字面量只能指定已知属性,而 __ 类型中不存在 __