The setup:
In [11]: import mock
In [12]: stuff = mock.MagicMock()
In [13]: stuff.__contains__.return_value = False
In [14]: "thing" in stuff
Out[14]: False
In [15]: stuff.mock_calls
Out[15]: [call.__contains__('thing')]
Then, say all that's in a unit test, and afterwards:
self.assertListEqual(stuff.mock_calls, [call.__contains__('thing')])
And you get:
AssertionError: Lists differ: [call.__contains__('thing')] != [False]
The __contains__
magic method of mock.call
get executed, returning False
. To get around this, use assert_called_with
:
stuff.__contains__.assert_called_with('thing')