mardi 21 juillet 2015

Cannot patch requests post

I can't patch requests post method. I read http://ift.tt/1jbZEld . But didn't understand where I made the error.

structure

tests.py
package
    __init__.py
    module.py

package/module.py

import requests
import mock

class Class(object):
    def send_request(self):
        ...
        response = requests.post(url, data=data, headers=headers)
        return response

tests.py

@mock.patch('package.module.requests.post')
def some_test(request_mock):
    ...
    data = {...}
    request_mock.return_value = data
    # invoke some class which create instance of Class
    # and invokes send_request behind the scene
    request_mock.assert_called_once_with()

traceback

Traceback (most recent call last):
  File "tests.py", line 343, in some_test
    request_mock.assert_called_once_with()
  File "/home/discort/python/project/env/local/lib/python2.7/site-packages/mock/mock.py", line 941, in assert_called_once_with
    raise AssertionError(msg)
  AssertionError: Expected 'post' to be called once. Called 0 times.

Aucun commentaire:

Enregistrer un commentaire