I am trying to mock a return value for a function that I am calling, with the help of pytest and monkeypatching.
I set up the fixture for my mock class, and I am trying to "overwrite" one of the methods in said class.
from foggycam import FoggyCam
from datetime import datetime
@pytest.fixture
def mock_foggycam():
return Mock(spec=FoggyCam)
def test_start(mock_foggycam, monkeypatch):
def get_mock_cookie():
temp = []
temp.append(Cookie(None, 'token', '000000000', None, None, 'somehost.com',
None, None, '/', None, False, False, 'TestCookie', None, None, None))
return temp
monkeypatch.setattr(FoggyCam, 'get_unpickled_cookies', get_mock_cookie)
cookies = mock_foggycam.get_unpickled_cookies()
mock_foggycam.get_unpickled_cookies.assert_called_with()
for pickled_cookie in cookies:
mock_foggycam.cookie_jar.set_cookie(pickled_cookie)
However, I might be missing something, because calling assert_called_with throws an error:
________________________________________________________________ test_start ________________________________________________________________
mock_foggycam = <Mock spec='FoggyCam' id='4408272488'>, monkeypatch = <_pytest.monkeypatch.MonkeyPatch object at 0x106c0e5c0>
def test_start(mock_foggycam, monkeypatch):
def get_mock_cookie():
temp = []
temp.append(Cookie(None, 'token', '000000000', None, None, 'somehost.com',
None, None, '/', None, False, False, 'TestCookie', None, None, None))
return temp
monkeypatch.setattr(mock_foggycam, 'get_unpickled_cookies', get_mock_cookie)
cookies = mock_foggycam.get_unpickled_cookies()
> mock_foggycam.get_unpickled_cookies.assert_called_with()
E AttributeError: 'function' object has no attribute 'assert_called_with'
Is there something in my monkeypatching logic that I am misplacing?
monkeypatch.setattr(FoggyCam, ...vsmonkeypatch.setattr(mock_foggycam, ...assert_called_with) and also keeps the original behavior of yourget_mock_cookie(a function). You can try something likemonkeypatch.setattr(mock_foggycam, "get_unpickled_cookies", Mock(wraps=get_mock_cookie)).