You have a class whose API you want to verify:
class Class_A(object): def method_X(self): self.method_Y() def method_Y(self): pass
You write the following test fully expecting it to pass. Alas, the
method_Y assertion fails.
def test_class_a(self): obj = Class_A() mock = MagicMock(spec_set=obj, wraps=obj) mock.method_X() mock.method_Y.assert_called_once_with()
This is because the Python Unittest Mock, while wrapping
mock.method_X(), Mock uses
obj in the
__self__ of the
method_Y binding. As a result the
self value passed to
method_X is that of the
obj and not
mock and the call to
method_Y is therefore invisible to the Mock.