<p dir="ltr">Hi.</p>
<p dir="ltr">Your issue is because you mutate ctx each time through the loop.</p>
<p dir="ltr">Mock isn&#39;t going to try arbitrarily copying the arguments that are passed to your functions/mocks, so even though your mock was correctly called, by the time you make your assertion the dict has been mutated and now contains the form from the second time through the loop.</p>

<p dir="ltr">Cheers,<br>
Julian</p>
<div class="gmail_quote">On May 20, 2013 6:56 AM, &quot;John Papanastasiou&quot; &lt;<a href="mailto:johnnypapps@gmail.com">johnnypapps@gmail.com</a>&gt; wrote:<br type="attribution"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div dir="ltr">Apologies, the code I posted was missing an important print statement.  I&#39;ve attached the corrected script and more useful sample output:<div><br></div><div>Regards,</div><div><br></div><div>
John.</div><div><br></div><div><div> ./nested_calls.py</div><div>..</div><div>Mock: top_level: &lt;mock.MagicMock object at 0x1005675d0&gt;</div><div>Mock: nested: &lt;mock.Mock object at 0x10059a990&gt;</div>
<div>Call Args, top_level.handler.trigger: [(({&#39;event&#39;: &lt;mock.Mock object at 0x10059a990&gt;},), {})]</div><div>Mock: nested: &lt;mock.Mock object at 0x10059a990&gt;</div><div>Call Args, nested.handler.trigger: [(({&#39;event&#39;: &lt;mock.Mock object at 0x10059a990&gt;},), {})]</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 &quot;./nested_calls.py&quot;, line 54, in test_top_level_and_extra</div><div>    top_level.handler.trigger.assert_called_once_with(ctx)</div><div>AssertionError: Expected: (({&#39;event&#39;: &lt;mock.MagicMock object at 0x1005675d0&gt;},), {})</div>

<div>Called with: (({&#39;event&#39;: &lt;mock.Mock object at 0x10059a990&gt;},), {})</div><div><br></div><div>----------------------------------------------------------------------</div><div>Ran 3 tests in 0.059s</div><div>

<br></div><div>FAILED (failures=1)</div></div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Mon, May 20, 2013 at 11:52 AM, John Papanastasiou <span dir="ltr">&lt;<a href="mailto:johnnypapps@gmail.com" target="_blank">johnnypapps@gmail.com</a>&gt;</span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><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>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><br></div>
<div>To demonstrate this, I have written a script which you can just run.</div><div><br></div><div>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 &#39;event&#39; set to the event instance).<br>


</div><div><br></div><div>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><br></div><div>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><br></div><div>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&#39;ve pasted a sample output below.</div>


<div><br></div><div>Is this a defect in the mock.call_args_list or have I misunderstood the documentation?</div><div><br></div><div>Any help you can give is greatly appreciated.</div><div><br>
</div><div>Regards,</div><div><br></div><div>John.</div><div><br></div><div>Sample Output:</div><div><br></div><div><div>$ ./nested_calls.py</div><div>..</div><div>Mock: top_level: &lt;mock.MagicMock object at 0x100567590&gt;</div>


<div>Call Args, top_level.handler.trigger: [(({&#39;event&#39;: &lt;mock.Mock object at 0x10059a950&gt;},), {})]</div><div>Mock: nested: &lt;mock.Mock object at 0x10059a950&gt;</div><div>Call Args, nested.handler.trigger: [(({&#39;event&#39;: &lt;mock.Mock object at 0x10059a950&gt;},), {})]</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 &quot;./nested_calls.py&quot;, line 53, in test_top_level_and_extra</div><div>    top_level.handler.trigger.assert_called_once_with(ctx)</div><div>AssertionError: Expected: (({&#39;event&#39;: &lt;mock.MagicMock object at 0x100567590&gt;},), {})</div>


<div>Called with: (({&#39;event&#39;: &lt;mock.Mock object at 0x10059a950&gt;},), {})</div><div><br></div><div>----------------------------------------------------------------------</div><div>Ran 3 tests in 0.059s</div><div>


<br></div><div>FAILED (failures=1)</div></div><div><br></div><div><br></div><div><br></div></div>
</blockquote></div><br></div>
<br>_______________________________________________<br>
testing-in-python mailing list<br>
<a href="mailto:testing-in-python@lists.idyll.org">testing-in-python@lists.idyll.org</a><br>
<a href="http://lists.idyll.org/listinfo/testing-in-python" target="_blank">http://lists.idyll.org/listinfo/testing-in-python</a><br>
<br></blockquote></div>