I want to use patch to record all function calls made to a function in a class for a unittest, but need the original function to still run as expected. I created a dummy code example below:
from mock import patch
class A(object):
def __init__(self):
self._a = 1
class B(A):
def __init__(self):
super(B, self).__init__() # TypeError: super() argument 1 must be type, not MagicMock
self._b = 11
def bar(self, b):
self._b = self._b + 1 + b
def foo(self, b):
self.bar(b)
class MockB(B):
def foo(self, b):
super(MockB, self).foo(self, b)
@patch('main.B')
def main(b_class):
b_class.side_effect = MockB
b = B()
print b._b # 11
b.foo(0)
print b._b # 12
main()
In my case, the instance of the class b = B()
is not actually in the main function but in another module, so I can't Mock the instance. I need it to generically be a decorator for all instances of B.
Summary: I am not sure how to individually mock the class method on it's own, but still call the original method. After, I want to use something like call_args_list where I can see all calls made to foo()
.