Why don't my Django unittests know that MessageMiddleware is installed?

前端 未结 9 691
傲寒
傲寒 2020-12-14 05:54

I\'m working on a Django project and am writing unittests for it. However, in a test, when I try and log a user in, I get this error:

MessageFailure: You can         


        
9条回答
  •  时光说笑
    2020-12-14 06:48

    If you're seeing a problem in your Middleware, then you're not doing "Unit Test". Unit tests test a unit of functionality. If you interact with other parts of your system, you're making something called "integration" testing.

    You should try to write better tests, and this kind of problems shouldn't arise. Try RequestFactory. ;)

    def test_some_view(self):
        factory = RequestFactory()
        user = get_mock_user()
        request = factory.get("/my/view")
        request.user = user
        response = my_view(request)
        self.asssertEqual(status_code, 200)
    

提交回复
热议问题