Conchylicultor
Conchylicultor

Reputation: 5739

How to get `self` instance in `mock.Mock().call_args`?

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 ?

Upvotes: 10

Views: 2324

Answers (2)

Benoit Blanchon
Benoit Blanchon

Reputation: 14571

with mock.patch.object(A, 'f', autospec=True) as mock_obj:
    A().f(1, 2)

print(mock_obj.call_args_list)  # [call(<__main__.A object at 0x7fb2908fc880>, 1, 2)]

From the documentation:

If you pass autospec=True [...] the mocked function will be turned into a bound method if it is fetched from an instance. It will have self passed in as the first argument [...]

Upvotes: 15

progmatico
progmatico

Reputation: 4964

From the Python console, help(mock.MethodType):

Create a bound instance method object.

from unittest import mock

class A:

  def f(self, *args, **kwargs):
    pass


mock_obj = mock.Mock()
a = A()
with mock.patch.object(A, 'f', mock_obj):
    a.f(1, 2)
    print(mock_obj.call_args)

# fixed self in call_args by patching
# with a bound instance method mock
mock_obj = mock.Mock()
with mock.patch.object(A, 'f', mock.MethodType(mock_obj, a)):
    a.f(1, 2)
    print(mock_obj.call_args)

Output:

call(1, 2)
call(<__main__.A object at 0x7ff8223e1dc0>, 1, 2)

Upvotes: 1

Related Questions