mardi 9 avril 2019

mocking a post method in unittest doesn't work

I've write some code for my website that send a verification sms to users,the code works correct but now I want to test it and need to mock sending code, but test doesn't work.

here is all of my code: I have a view that send a verification code to user like the following:

view.py
class SendCode(GenericAPIView):
    def post(self, request, *args, **kwargs):
        """some of code that send messages"

I've write a test that doesn't work:

@mock.patch('view.SendCode.post', autospec=True)
class MyTest(unittest.TestCase):
    def setUp(self):
        self.client = Client()

    def test_get_data(self, mock_call_external_api):
        data = {'phone': '11111111'}
        self.client.post('/send-code/',json.dumps(data),                                       content_type='application/json')
        self.assertTrue(mock_call_external_api.called)

and here i've got the following error:

AssertionError: Expected a `Response`, `HttpResponse` or   `HttpStreamingResponse` to be returned from the view, but received a `<class 'unittest.mock.MagicMock'>

I've tried lots of solution such as changing the patch path but it doesn't really work. thanks in advance for any help

Aucun commentaire:

Enregistrer un commentaire