Skip to content

Instantly share code, notes, and snippets.

@jrobichaud
Last active March 15, 2019 17:48
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jrobichaud/6f2f31fc1584d70eec3335ce956fba65 to your computer and use it in GitHub Desktop.
Save jrobichaud/6f2f31fc1584d70eec3335ce956fba65 to your computer and use it in GitHub Desktop.
Code snippet on how to implement appropriate method mocking of inaccessible instance.
import unittest.mock
class Callee:
def __init__(self, member):
self.member = member
def do_something(self, method_argument):
pass
def do_call():
Callee("Bar").do_something("Foo")
mocked_do_something_method = unittest.mock.Mock(wraps=Callee.do_something)
with unittest.mock.patch.object(Callee, 'do_something', new=lambda *args, **kwargs: mocked_do_something_method(*args, **kwargs)):
do_call()
assert mocked_do_something_method.call_count == 1
method_args, method_kwargs = mocked_do_something_method.call_args
callee_self, callee_method_argument = method_args
assert callee_self.member == "Bar"
assert callee_method_argument == "Foo"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment