<div dir="ltr">Hi,<div><br></div><div>I believe I have found a problem with mock.call_args_list. My understanding is that for a given mock, if the function you are testing makes multiple calls to a function on that mock, the call_args_list is set to a list of the successive argument lists for each call.</div>
<div><br></div><div style>The problem I have is that the call_args_list for one mock is being replaced with the call_args_list for a second mock that is being used in the same function under test.</div><div style><br></div>
<div style>To demonstrate this, I have written a script which you can just run.</div><div style><br></div><div style>I have an event object which has a property which may be set to a list of extra event objects. Event objects have a property called handler which itself has a function trigger which called with a context (a dict with a key 'event' set to the event instance).<br>
</div><div style><br></div><div style>The function i am trying to test triggers the handler on the top level event, then goes through each event in the list triggering their handlers. To test this function, I am making assertions about the calls to the respective trigger functions.<br>
</div><div style><br></div><div style>The attached code demonstrates that the call_args_list for the top_level event (the one being called first in the function) is set to the call_args_list for the extra event (the one being called second). </div>
<div style><br></div><div style>The TestCase includes two unit tests which test the calls to the top_level event trigger and the nested event trigger independently and these pass. The third unit test, test_top_level_and_extra prints out some information about the mocks involved and the call_args_lists. This output demonstrates the error I am seeing. I've pasted a sample output below.</div>
<div style><br></div><div style>Is this a defect in the mock.call_args_list or have I misunderstood the documentation?</div><div style><br></div><div style>Any help you can give is greatly appreciated.</div><div style><br>
</div><div style>Regards,</div><div style><br></div><div style>John.</div><div style><br></div><div style>Sample Output:</div><div style><br></div><div style><div>$ ./nested_calls.py</div><div>..</div><div>Mock: top_level: <mock.MagicMock object at 0x100567590></div>
<div>Call Args, top_level.handler.trigger: [(({'event': <mock.Mock object at 0x10059a950>},), {})]</div><div>Mock: nested: <mock.Mock object at 0x10059a950></div><div>Call Args, nested.handler.trigger: [(({'event': <mock.Mock object at 0x10059a950>},), {})]</div>
<div><br></div><div>F</div><div>======================================================================</div><div>FAIL: test_top_level_and_extra (__main__.TestNestedCalls)</div><div>----------------------------------------------------------------------</div>
<div>Traceback (most recent call last):</div><div> File "./nested_calls.py", line 53, in test_top_level_and_extra</div><div> top_level.handler.trigger.assert_called_once_with(ctx)</div><div>AssertionError: Expected: (({'event': <mock.MagicMock object at 0x100567590>},), {})</div>
<div>Called with: (({'event': <mock.Mock object at 0x10059a950>},), {})</div><div><br></div><div>----------------------------------------------------------------------</div><div>Ran 3 tests in 0.059s</div><div>
<br></div><div>FAILED (failures=1)</div></div><div style><br></div><div style><br></div><div style><br></div></div>