我正在尝试为我的 django 应用程序编写一些测试,但它抛出了一个错误:
File "/Users/croberts/.virtualenvs/litem/lib/python3.4/site-packages/django/contrib/auth/__init__.py", line 101, in login
if SESSION_KEY in request.session:
AttributeError: 'WSGIRequest' object has no attribute 'session'
这是我要运行的代码:
class SimpleTest(TestCase):
def setUp(self):
self.request_factory = RequestFactory()
def test_signup(self):
request = self.request_factory.post("/signup/", {
"email": "email@email.com",
"password": "password",
"password-confirm": "password",
"firm": "big law firm"})
response = signup_user(request)
user = User.objects.get(email="email@email.com")
self.assertEqual(user.username, "email@email.com")
self.assertEqual(user.firm, "big law firm")
self.assertEqual(response.status_code, 302) #if it's successful it redirects.
这是我的中间件:
MIDDLEWARE_CLASSES = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
和我安装的应用程序:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'patents',
]