Search code examples
pythonunit-testingmockingpython-mock

assert_has_calls does not stub the method call


I'm using assert_has_calls from the mock python library. I'm running into a problem when I do something like this:

mocks = mock.Mock()
mocks.assert_has_calls([mock.call.method_to_be_mocked(mock.ANY),
                        mock.call.method_to_be_mocked(mock.ANY)])

I want to verify that the method is called a certain number of times, but at the same time I also want to stub out the calls to the method. assert_has_calls doesn't seem to do the stubbing part. The actual method is called and it fails in the dev environment.

What can I do to resolve this ?


Solution

  • You want to assert on how many times it was called, so use call_count and use assert_equal (from unittest or whichever other equivalent from the testing framework you are using) to validate that it equates to the number of times you expect it to be called:

    assert_equal(mock.call.method_to_be_mocked.call_count, 2)