Last active
March 15, 2019 17:48
-
-
Save jrobichaud/6f2f31fc1584d70eec3335ce956fba65 to your computer and use it in GitHub Desktop.
Code snippet on how to implement appropriate method mocking of inaccessible instance.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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