[Fixed]-Django Patch Side effect IntegrityError not being raised

1👍

✅

Your view catches IntegrityError and returns a redirect. The client post, which is what your test calls, doesn’t raise an error at all.

Rather, you should be testing that the actions in the except block happen: is the warning set, and does the post redirect to the draft schedule instead of the frozen one.

0👍

If you want to really test that your function throws the exception, you need to test the function directly rather than through the POST. Something like:

from django.test import RequestFactory

@patch('reports.tasks.create_frozen_schedule')
def test_freeze_schedule_with_conflict(self, mock_freeze):
    mock_freeze.side_effect = IntegrityError
    myrequest = RequestFactory().post(*some_url*)
    # you may need to add, e.g., items to myrequest.POST
    with self.assertRaises(IntegrityError):@patch('reports.tasks.create_frozen_schedule')
        freeze_schedule(myrequest, pk)

Leave a comment