I'm writing some tests for a function which passes an instance of collections.Counter to another function which I need to mock. Here is a small example representing my code:
# 'bar' module
def callback(counter):
"""Do something with the *counter*."""
pass
def update():
"""Update counter and pass it to callback."""
counter = collections.Counter()
for _ in range(3):
counter.update(["foo"])
callback(counter)
The following test does not pass as the same counter instance has been mutated and passed to the 'callback' function. Therefore, the assertion will only pass when all calls take the latest value of the counter, which is collections.Counter({"foo": 3})
@pytest.fixture()
def mocked_callback(mocker):
return mocker.patch("bar.callback")
def test_update(mocker, mocked_callback):
update()
assert mocked_callback.call_args_list == [
mocker.call(collections.Counter({"foo": 1})),
mocker.call(collections.Counter({"foo": 2})),
mocker.call(collections.Counter({"foo": 3})),
]
Does anyone know a good way to analyze the exact state of the mutated object when it is passed to the mocked function?