[Django]-Mock.assert_has_calls() not working as expected

4👍

You are passing the result of Mock.call to assert_has_calls. But, assert_has_calls expects a top-level call object.


Note, after reading the docs I concur this is not overly clear. Here’s the docs for assert_has_calls:

assert the mock has been called with the specified calls. The mock_calls list is checked for the calls.

If any_order is false then the calls must be sequential. There can be extra calls before or after the specified calls.

If any_order is true then the calls can be in any order, but they must all appear in mock_calls.

mock = Mock(return_value=None)
mock(1)
mock(2)
mock(3)
mock(4)
calls = [call(2), call(3)]
mock.assert_has_calls(calls)
calls = [call(4), call(2), call(3)]
mock.assert_has_calls(calls, any_order=True)

It’s not clear where the call object comes from in that example. I can see the confusion.

It’s only when you see the call documentation that it gets a bit clearer (emphasis mine):

call() is a helper object for making simpler assertions, for comparing with call_args, call_args_list, mock_calls and method_calls. call() can also be used with assert_has_calls().

Leave a comment