Django 1.9.6
我想写一些单元测试来检查重定向。
你能帮我理解我在这里做错了什么吗?
先感谢您。
考试:
from django.test import TestCase
from django.core.urlresolvers import reverse
from django.http.request import HttpRequest
from django.contrib.auth.models import User
class GeneralTest(TestCase):
def test_anonymous_user_redirected_to_login_page(self):
user = User(username='anonymous', email='vvv@mail.ru', password='ttrrttrr')
user.is_active = False
request = HttpRequest()
request.user = user
hpv = HomePageView()
response = hpv.get(request)
self.assertRedirects(response, reverse("auth_login"))
结果:
错误:test_anonymous_user_redirected_to_login_page (general.tests.GeneralTest)
回溯(最后一次调用):文件“/home/michael/workspace/photoarchive/photoarchive/general/tests.py”,第 44 行,在 test_anonymous_user_redirected_to_login_page self.assertRedirects(response,reverse(“auth_login”))文件“/home /michael/workspace/venvs/photoarchive/lib/python3.5/site-packages/django/test/testcases.py”,第 326 行,在 assertRedirects redirect_response = response.client.get(path, QueryDict(query), AttributeError: 'HttpResponseRedirect' 对象没有属性 'client'
在 0.953 秒内运行 3 次测试
什么 pdb 说:
-> self.assertRedirects(response, reverse("auth_login"))
(Pdb) response
<HttpResponseRedirect status_code=302, "text/html; charset=utf-8", url="/accounts/login/">