mardi 30 mai 2017

Side effect exception stopping test execution

I have the following situation:

I'm testing a Django view, and I wrote a test in order to cover the flow when some exception have been raised.

The view looks like something like this:

class SomeView(View):
   # ...
   def get(self, *args, **kwargs):
       try:
           some_function_that_must_except()
       except Exception as error:
           # Flow I want to cover with test.
       else:
           # Flow already covered

And the test:

@mock.patch('some_function_that_must_except')
def test_redirect_logic_fail(self, mock_some_function_that_must_except):
    # Configuring mock
    mock_some_function_that_must_except.side_effect = Exception('Exception raised on purpose')

    response = self.client.get(
        reverse('the-view-im-testing')
    )
    # If the exception is raised then we should be redirected to
    # another view.
    self.assertRedirects(response, reverse('alternate-view'))

So good, so far, but when I run this test I get this error message:

line 1118, in _mock_call raise effect Exception: Exception raised on purpose

and then the test stops.

Why is this exception not been handled by my view?

I'm using mock 2.0.0 and python 2.7.12.

Aucun commentaire:

Enregistrer un commentaire