jeudi 19 juillet 2018

Mock patch object is not called

I am trying to patch two methods that are called in another class' post() method. I want the patches to replace the method calls( _insert() and send() ) in the post() method, which is defined under SMSHandler class. Here's the test code I wrote so far,

@patch.object(BaseHandler, '_insert', return_value=InsertOneResult)
@patch.object(SmsSender, 'send', return_value=True)
def test_post(self, patch_handler, patch_sender):
    patch_handler._insert.return_value = InsertOneResult
    patch_sender.send.return_value = True

    req_json = {....}
    postreq = requests.post('http://localhost:8888/sms', json=req_json)

    saved = (some object)

    patch_handler.assert_called_with(saved, "sms")
    patch_sender.assert_called_with()
    self.assertTrue(postreq.ok)

And original method calls are as follows (in post() method)

Sender.factory("sms").send(saved)

(...)

result = yield super(SMSHandler, self)._insert(saved, "sms")

Problem is, patch_handler and patch_sender are not called AT ALL. So I wonder why they do not replace the method calls, and how to do that.

Aucun commentaire:

Enregistrer un commentaire