Skip to content

Instantly share code, notes, and snippets.

@lroggendorff
Last active August 29, 2015 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lroggendorff/059938d665de90c29290 to your computer and use it in GitHub Desktop.
Save lroggendorff/059938d665de90c29290 to your computer and use it in GitHub Desktop.
So this is fun...

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')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment