[issue32092] mock.patch with autospec does not consume self / cls argument

Claudiu Belu report at bugs.python.org
Mon Nov 20 10:50:21 EST 2017


New submission from Claudiu Belu <cbelu at cloudbasesolutions.com>:

Currently, the autospec=True argument can be passed to mock.patch, but when trying to make assertions on the call and its arguments, it can fail, as it expects an instance / class instance reference as the first argument (self / cls arguments are not consumed when autospec-ing).

Steps to reproduce:

>>> import mock
>>> from mock.tests import testmock
>>>
>>> with mock.patch.object(testmock.Something, 'meth', autospec=True):
...     smth = testmock.Something()
...     smth.meth(mock.sentinel.a, mock.sentinel.b, mock.sentinel.c, mock.sentinel.d)
...     smth.meth.assert_called_once_with(mock.sentinel.a, mock.sentinel.b, mock.sentinel.c, mock.sentinel.d)
...
<MagicMock name='meth()' id='139792133225424'>
Traceback (most recent call last):
  File "<stdin>", line 4, in <module>
  File "/usr/local/lib/python2.7/dist-packages/mock/mock.py", line 318, in assert_called_once_with
    return mock.assert_called_once_with(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/mock/mock.py", line 948, in assert_called_once_with
    return self.assert_called_with(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/mock/mock.py", line 937, in assert_called_with
    six.raise_from(AssertionError(_error_message(cause)), cause)
  File "/usr/local/lib/python2.7/dist-packages/six.py", line 718, in raise_from
    raise value
AssertionError: Expected call: meth(sentinel.a, sentinel.b, sentinel.c, sentinel.d)
Actual call: meth(<mock.tests.testmock.Something object at 0x7f23e456dcd0>, sentinel.a, sentinel.b, sentinel.c, sentinel.d)

Expected result: no AssertionError.

----------
messages: 306559
nosy: cbelu
priority: normal
severity: normal
status: open
title: mock.patch with autospec does not consume self / cls argument
versions: Python 2.7, Python 3.5, Python 3.6, Python 3.7, Python 3.8

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue32092>
_______________________________________


More information about the Python-bugs-list mailing list