Django Testing - check messages for a view that redirects

后端 未结 5 880
我在风中等你
我在风中等你 2021-02-02 10:16

I have been writing tests for one of my django applications and have been looking to get around this problem for quite some time now. I have a view that sends messages using

5条回答
  •  长情又很酷
    2021-02-02 10:37

    Alternative method mocking messages (doesn't need to follow redirect):

    from mock import ANY, patch
    from django.contrib import messages
    
    @patch('myapp.views.messages.add_message')
    def test_some_view(self, mock_add_message):
        r = self.client.get('/some-url/')
        mock_add_message.assert_called_once_with(ANY, messages.ERROR, 'Expected message.')  # or assert_called_with, assert_has_calls...
    

提交回复
热议问题