6

我正在尝试为我的 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',
]
4

3 回答 3

12

尝试使用测试客户端而不是请求工厂。这也具有测试您的 URL 配置的优势。

class SimpleTest(TestCase):
    def test_signup(self):
        response = self.client.post("/signup/", {
            "email": "email@email.com", 
            "password": "password", 
            "password-confirm": "password", 
            "firm": "big law firm"})
        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.
于 2016-02-26T21:08:05.797 回答
7

对于使用 Django 2.0 及更高版本的任何人 - RequestFactory() 无权访问中间件:

它不支持中间件。如果视图正常运行需要,会话和身份验证属性必须由测试本身提供。

https://docs.djangoproject.com/en/2.2/topics/testing/advanced/#the-request-factory

因此需要手动添加如下:

from django.contrib.sessions.middleware import SessionMiddleware

class SimpleTest(TestCase):
    def setUp(self):
        self.request_factory = RequestFactory()

        middleware = SessionMiddleware()
        middleware.process_request(self.request_factory)
        self.request_factory.session.save()

    def test_signup(self):
        ...
于 2020-06-02T07:57:26.507 回答
5

在 django 3.1 中,在遵循@Alasdair 的回答后,我得到了这个错误 'AttributeError: 'RequestFactory' object has no attribute 'COOKIES'。我首先通过初始化请求解决了这个问题,下面是代码片段。

def setUp(self):
    request = RequestFactory().get('/')
    middleware = SessionMiddleware()
    middleware.process_request(request)
    request.session.save()

编辑 4.0 版 sessionmiddlewre 需要一个位置参数:'get_response' 所以:

def setUp(self):
    request = RequestFactory().get('/')
    middleware = SessionMiddleware(request)
    middleware.process_request(request)
    request.session.save()
    

于 2020-11-26T08:51:05.263 回答