Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
class Foo():
message = 'foo'
def bar(self):
print('bar')
from unittest.mock import patch
def baz(self):
print(self.message)
@patch.object(Foo, 'bar', autospec=True, side_effect=baz)
# ^^^^^^^^^^^^^
# this is essential
def test(mock_bar):
foo = Foo()
foo.bar()
assert mock_bar.call_count == 1
test()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment