这是关于Django 1.6的内容
def test_perfil_password_validates_new_passwords(self):
self.client.login(username='test@test.com', password='test')
resp = self.client.post('/perfil/password/',
json.dumps({'oldpassword': 'test',
'newPassword1': 'wrong',
'newPassword2': 'nuevo'}
),
'text/json',
HTTP_X_REQUESTED_WITH='XMLHttpRequest')
self.assertEqual(resp.status_code, 400)
这个是可行的,下面是输出结果:
Creating test database for alias 'default'... .
----------------------------------------------------------------------
Ran 1 test in 0.273s
OK
现在如果我添加这个额外的断言。
self.assertContains(resp, '"error":')
Creating test database for alias 'default'... F
======================================================================
FAIL: test_perfil_password_validates_new_passwords (users.tests.PerfilLoggedTestCase)
----------------------------------------------------------------------
Traceback (most recent call last): File "/.../src/users/tests.py",
line 141, in test_perfil_password_validates_new_passwords
self.assertContains(resp, '"error":')
File "/usr/local/lib/python2.7/dist-packages/django/test/testcases.py", line 327,
in assertContains
" (expected %d)" % (response.status_code, status_code))
AssertionError: Couldn't retrieve content: Response code was 400 (expected 200)
----------------------------------------------------------------------
Ran 1 test in 0.241s
FAILED (failures=1)
我不知道为什么这里期望返回200,也不知道为什么会报assertionError
的错误。我甚至可以打印出内容,所以它确实存在。我错过了什么吗?