question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Mock(2.0.0).assert_has_calls() raise AssertionError in two same calls

See original GitHub issue

hello, i use Mock (2.0.0) in python2.7,and found this issue.

Example: 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)])

python test.py 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() [TypeError(‘too many positional arguments’,)]

issue in cpython: http://bugs.python.org/issue26752

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
lanzzcommented, Jun 22, 2016

Here is another example that triggers this issue:

import mock
import unittest


class Foo(object):

    def __init__(self, a):
        pass

    def bar(self, a, b):
        return (b, a)


class FooTestCase(unittest.TestCase):

    def test_foo(self):
        mock_class = mock.Mock(spec_set=Foo)
        foo = mock_class(1)
        foo.bar(2, 3)
        mock_class.assert_has_calls(
            [
                mock.call(1),
                mock.call().bar(2, 3),
            ]
        )

Test run output:

$ ./bin/python -m unittest test
F
======================================================================
FAIL: test_foo (test.FooTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "test.py", line 23, in test_foo
    mock.call().bar(2, 3),
  File ".../lib/python2.7/site-packages/mock/mock.py", line 969, in assert_has_calls
    ), cause)
  File ".../lib/python2.7/site-packages/six.py", line 718, in raise_from
    raise value
AssertionError: Calls not found.
Expected: [call(1), call().bar(2, 3)]
Actual: [call(1), call().bar(2, 3)]

----------------------------------------------------------------------
Ran 1 test in 0.005s

FAILED (failures=1)

What I’ve found is that mock_class._call_matcher attempts to bind the bar(2, 3) call to mock_class._spec_signature, but that’s the signature of the Foo class (Foo.__init__(self, a), single positional argument), not that of the Foo.bar method (Foo.bar(self, a, b), two positional arguments), thus it returns TypeError('too many positional arguments') instead. If I drop the spec from the mock, the assertion succeeds, so it is not really a case of calls not matching but obviously a conflict with the spec itself.

2reactions
lanzzcommented, Nov 29, 2018

I love it how it took two years and a half to close this issue with “report it upstream instead” 🙄

Read more comments on GitHub >

github_iconTop Results From Across the Web

Python's assert_has_calls throws AssertionError that calls ...
I'm getting two of them: one mock_producer in the test class and a second mocked producer called myproducer (the name I used in...
Read more >
Issue 43371: Mock.assert_has_calls works strange
When I call Mock.assert_has_calls with any_order=False it checks that expected calls are the same calls as performed on mock and raise an ...
Read more >
python's assert_has_calls throws assertionerror ... - splunktool
I'm getting two of them: one mock_producer in the test class and a second ... in raise_from raise value AssertionError: Calls not found....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found