Django单元测试:AttributeError:'WSGIRequest'对象没有属性'user' [英] Django unit testing: AttributeError: 'WSGIRequest' object has no attribute 'user'

查看:160
本文介绍了Django单元测试:AttributeError:'WSGIRequest'对象没有属性'user'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

运行测试时,我输出以下错误:

When running the tests i get outputted following error:

user = self.request.user
AttributeError:'WSGIRequest'对象没有属性'user '

user = self.request.user AttributeError: 'WSGIRequest' object has no attribute 'user'

我尝试从 MIDDLEWARE 切换到 MIDDLEWARE_CLASSES 反之亦然。目前,我正在运行Django 2.1。

I have tried switching from MIDDLEWARE to MIDDLEWARE_CLASSES and vice versa. Currently, I'm running Django 2.1.

此外,我尝试过切换中间件的位置,但无济于事。

Also, I have tried switching middleware positions and it didn't help.

MIDDLEWARE = (
    "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",
    "django.middleware.security.SecurityMiddleware",
    "whitenoise.middleware.WhiteNoiseMiddleware",  # to serve static files
)



test_views.py



test_views.py

from django.test import (TestCase,
                     RequestFactory
                     )
from mixer.backend.django import mixer

from .. import views
from accounts.models import User


class TestHomeView(TestCase):
    def test_anonymous(self):
        req = RequestFactory().get("/")
        resp = views.HomeView.as_view()(req)
        self.assertEquals(resp.status_code, 200,
                          "Should be callable by anyone")

    def test_auth_user(self):
        req = RequestFactory().get("/")
        req.user = mixer.blend("accounts.User")
        resp = views.HomeView.as_view()(req)
        self.assertTrue("dashboard" in resp.url,
                        "Should redirect authenticated user to /dashboard/")



错误输出



user = self.request.user
AttributeError:'WSGIRequest'对象没有属性'user'

error output

user = self.request.user AttributeError: 'WSGIRequest' object has no attribute 'user'

推荐答案

在第一个测试 test_anonymous 中,您实际上并没有设置用户。根据文档 https://docs.djangoproject。 com / zh-CN / 2.1 / topics / testing / advanced /#the-request-factory ,如果您使用 RequestFactory 并且必须进行设置,则不会执行中间件用户明确。因此,如果要再次测试 AnonymousUser ,则应设置 request.user = AnonymousUser()。如果您不这样做,则请求对象将没有 user 属性,因此将出现错误。

In the first test test_anonymous, you are not actually setting the user. As per documentation https://docs.djangoproject.com/en/2.1/topics/testing/advanced/#the-request-factory, the middleware are not executed if you use RequestFactory and you have to set the user explicitly. So if you want to test again AnonymousUser, you should set request.user = AnonymousUser(). If you don't do this, the request object will not have the user attribute, and thus the error.

这篇关于Django单元测试:AttributeError:'WSGIRequest'对象没有属性'user'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆