I observe an inconsistent behavior when patching a dummy class:
class A:
def f(self, *args, **kwargs):
pass
If I patch manually the function:
call_args_list = []
def mock_fn(*args, **kwargs):
call_args_list.append(mock.call(*args, **kwargs))
with mock.patch.object(A, 'f', mock_fn):
A().f(1, 2)
print(call_args_list) # [call(<__main__.A object at 0x7f0da0c08b50>, 1, 2)]
As expected mock_fn is called with the self argument (mock_fn(self, 1, 2)).
However, if I'm using a mock.Mock object, somehow the self argument is removed from the call:
mock_obj = mock.Mock()
with mock.patch.object(A, 'f', mock_obj):
A().f(1, 2)
print(mock_obj.call_args_list) # [call(1, 2)]
This feel inconsistent. mock_obj is called as mock_obj(self, 1, 2), yet mock_obj.call_args == call(1, 2). It removes the self argument from call_args. How can I access the bounded-method instance ?