classification
Title: Mock(2.0.0).assert_has_calls() raise AssertionError in two same calls
Type: behavior Stage:
Components: Versions: Python 3.4, Python 2.7
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: jekin000, jwdevel, michael.foord, rbcollins
Priority: normal Keywords:

Created on 2016-04-14 07:43 by jekin000, last changed 2017-02-20 17:48 by jwdevel.

Files
File name Uploaded Description Edit
test.py jekin000, 2016-04-14 07:43
Messages (3)
msg263375 - (view) Author: James (jekin000) * Date: 2016-04-14 07:43
>>> import mock
>>> print mock.__version__
2.0.0
>>> 

=================
test.py
from mock import Mock,call

class BB(object):
    def __init__(self):pass
    def print_b(self):pass
    def print_bb(self,tsk_id):pass


bMock = Mock(return_value=Mock(spec=BB))
bMock().print_bb(20)
bMock().assert_has_calls([call.print_bb(20)])


===================
Traceback (most recent call last):
  File "test.py", line 11, in <module>
    bMock().assert_has_calls([call.print_bb(20)])
  File "/usr/lib/python2.7/site-packages/mock/mock.py", line 969, in assert_has_calls
    ), cause)
  File "/usr/lib/python2.7/site-packages/six.py", line 718, in raise_from
    raise value
AssertionError: Calls not found.
Expected: [call.print_bb(20)]
Actual: [call.print_bb(20)]

=======
print expected in mock.py assert_has_calls()

result is:
[TypeError('too many positional arguments',)]
msg287728 - (view) Author: John W. (jwdevel) Date: 2017-02-13 21:54
This also seems to apply to unittest.mock in Python3.4.

I described my similar issue on SO: http://stackoverflow.com/questions/42212433/what-is-wrong-with-this-simple-py-test-use-case

It seems like it may be the same issue described here.

For reference, this is my repro case:

from unittest.mock import patch, call

    class Foo:
        def __init__(self):
            pass
        def my_method(self, value):
            pass
    
    def test_foo():
        with patch('test.Foo', autospec=True) as MockFoo:
            m = MockFoo()
            m.my_method(123)
            MockFoo.assert_has_calls([call(), call().my_method(123)])

It fails with:

    ...
    E               AssertionError: Calls not found.
    E               Expected: [call(), call().my_method(123)]
    E               Actual:   [call(), call().my_method(123)]

Which seems nonsensical to me.

Removing the `value` parameter and the `123` arguments in the test makes it pass(!)
msg288222 - (view) Author: John W. (jwdevel) Date: 2017-02-20 17:48
This got a little discussion over at http://lists.idyll.org/pipermail/testing-in-python/2017-February/007012.html

The current evidence seem to indicate this is indeed a bug in the implementation of assert_has_calls.
History
Date User Action Args
2017-02-20 17:48:35jwdevelsetmessages: + msg288222
2017-02-13 21:54:01jwdevelsetnosy: + jwdevel

messages: + msg287728
versions: + Python 3.4
2016-04-14 11:46:05SilentGhostsetnosy: + michael.foord
2016-04-14 07:43:24jekin000create