I'm having trouble testing that a method is called using mock -- as a simple example, let's say that the method is os.getcwd. I want to test that my own function, pickle_wdir, is calling os.getcwd as intended. However, the function I am testing pickles the value returned by os.getcwd, which results in an error.
Here's a simple example to reproduce the error.
os_ex.py:
import os
import pickle
def pickle_wdir(filename):
dir = os.getcwd()
with open(filename, 'wb') as handle:
pickle.dump(dir, handle)
test_os_ex.py:
from unittest import TestCase
from unittest.mock import patch
from os_ex import pickle_wdir
class TestPickleWdir(TestCase):
def test_os_called(self):
fname = 'dir.pickle'
with patch('os_ex.os') as mocked_obj:
pickle_wdir(fname)
mocked_obj.getcwd.assert_called()
The error message returned is
_pickle.PicklingError: Can't pickle <class 'unittest.mock.MagicMock'>: it's not the same object as unittest.mock.MagicMock.
How can I test that os.getcwd is called without getting this PicklingError?