我正在做一个django2.1实践项目。我测试的最后一行总是不及格。
如果发生重定向,assertTemplateUsed检查是否确实不起作用?
Traceback (most recent call last):
File "test_views.py", line 24, in test_home_page_not_login_redirect self.assertTemplateUsed(resp, 'users/home.html')
File "testcases.py", line 554, in assertTemplateUsed self.fail(msg_prefix + "No templates used to render the response")
AssertionError: No templates used to render the response
test_views.py
def test_home_page_not_login_redirect(self):
resp = self.client.get('/')
self.assertEqual(resp.status_code, 302)
self.assertRedirects(resp, '/accounts/login/?next=/')
self.assertTemplateUsed(resp, 'users/login.html')
settings.py
LOGOUT_REDIRECT_URL = '/accounts/login/'
因为repoy是302找到的重定向,所以没有用于HTTP响应的模板!在
如果您想真正遵循重定向链,可以将
follow=True
参数传递给self.client.get
(https://docs.djangoproject.com/en/2.1/topics/testing/tools/#django.test.Client.get)。通过这种方式,您将能够实际检查用于呈现(重定向)响应的模板。在相关问题 更多 >
编程相关推荐