Skip to content

Instantly share code, notes, and snippets.

@ikonst
Last active July 5, 2018 19:02
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 ikonst/9d1f60ec9585bcf38ad792299028df65 to your computer and use it in GitHub Desktop.
Save ikonst/9d1f60ec9585bcf38ad792299028df65 to your computer and use it in GitHub Desktop.
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