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

前端 未结 9 690
傲寒
傲寒 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)
    
    0 讨论(0)
  • 2020-12-14 06:50

    Tests create custom (tests) database. Maybe you have no messages there or something... Maybe you need setUp() fixtures or something?

    Need more info to answer properly.

    Why not simply do something like ? You sure run tests in debug mode right?

    # settings.py
    DEBUG = True
    
    from django.conf import settings
    # where message is sent:
    if not settings.DEBUG:
        # send your message ... 
    
    0 讨论(0)
  • 2020-12-14 06:51

    Do you only have one settings.py?

    0 讨论(0)
提交回复
热议问题