Dear Eric &amp; Carl<br><br>Thanks to both of you. Yes. Eric, your solution calls the mock version (I modified the assert a bit because, I think the the expected output is `args=[&#39;123&#39;]` rather than `args=&#39;123`).<br>
<br>The snippet of the old mock and new mock is found here: <a href="http://dpaste.org/vPswS/">http://dpaste.org/vPswS/</a><br><br>Observations:<br>1. test cases return `codebundle.tests`<br>2. inside render_reverse, it returns `codebundles.lib` (guess this means it is returning the local binding of `reverse`)<br>
<br>Yes. I am aware of the nice feature in 1.4, but I have to first verify them in my old Django (I am running 1.2.1.... omg)<br><br><br>&gt; The assertion of the result might be pointless, but it feels like it 
confirms what the code under test should produce. <br>&gt; Also, I realize I 
dropped the &quot;dummy_views&quot; from the argument simply b/c it seems 
superfluous in my example. <br>&gt; Obviously, if it necessary, then it is 
necessary and should be in the test… right?<br><br>Not at all. Like Carl said, it was pointless and bad to supply mock objects to the arguments in many cases. In this particular case, they are just `string type`. A dummy `foo` string will do it. I think these two assertions are good (although many people follow the one assertion per test rule). So if you want to assert that it did call with the args we passed in, for the sake of &quot;getting the right info of what goes wrong&quot;, we can write a test for each assertion. Actually, I am new to this mailing list so I created a duplicate. Carl explains this really well in his first reply: <a href="http://lists.idyll.org/pipermail/testing-in-python/2012-April/004913.html">http://lists.idyll.org/pipermail/testing-in-python/2012-April/004913.html</a><br>
<br><br>--<br>John<br><br>
<div class="gmail_quote">On Thu, Apr 12, 2012 at 3:46 PM, Eric Larson <span dir="ltr">&lt;<a href="mailto:eric@ionrock.org">eric@ionrock.org</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div class="HOEnZb"><div class="h5">On Thursday, April 12, 2012 at 2:18 PM, John Wong wrote:<br>
&gt; First, I hope I am not making duplicate (my first time using this kind of mailing list...)<br>
&gt;<br>
&gt; &gt; The first thing is to be clear in your mind which code you are intending<br>
&gt; &gt; to test. In this case, it sounds like you want to test your<br>
&gt; &gt; &quot;render_reverse&quot; function, but without exercising Django&#39;s &quot;reverse&quot;<br>
&gt; &gt; (which makes it a unit test). Mocking &quot;reverse&quot; is a reasonable way to<br>
&gt; &gt; go about this.<br>
&gt;<br>
&gt;<br>
&gt; Yes. Exactly. I want to perform a unittest on this function, which calls django&#39;s reverse (for whatever reason why I am doing this...)<br>
&gt;<br>
&gt; Here is the actual function from lib.py (there is no class involved, all functions in lib.py)<br>
&gt;<br>
&gt; from django.core.urlresolvers import reverse<br>
&gt; def render_reverse(f, kwargs):<br>
&gt; &quot;&quot;&quot;<br>
&gt; kwargs is a dictionary, usually of the form {&#39;args&#39;: [cbid]}<br>
&gt; &quot;&quot;&quot;<br>
&gt; return reverse(f, **kwargs)<br>
&gt;<br>
&gt; Because the name lookup is `reverse`, so I should patch `myproject.myapps.mylibrary.reverse` as opposed to `django.core.urlresolvers.reverse`<br>
&gt;<br>
&gt; I did the first method. I want to assert that `render_reverse` generates whatever `reverse` generates (by calling `reverse` directly). That&#39;s the goal. So I have the following test code:<br>
&gt;<br>
&gt; from lib import render_reverse, print_ls<br>
&gt;<br>
&gt; class LibTest(unittest.TestCase):<br>
&gt;<br>
&gt; def test_render_reverse_is_correct(self):<br>
&gt; with patch(&#39;myproject.myapps.mylibrary.reverse&#39;) as mock_reverse:<br>
&gt; from lib import render_reverse<br>
&gt; mock_f = MagicMock(name=&#39;f&#39;, return_value=&#39;dummy_views&#39;)<br>
&gt; mock_kwargs = MagicMock(name=&#39;kwargs&#39;,return_value={&#39;args&#39;:[&#39;123&#39;]})<br>
&gt; mock_reverse.return_value = &#39;/natrium/cb/details/123&#39;<br>
&gt; response = render_reverse(mock_f(), mock_kwargs())<br>
&gt; print mock_reverse.mock_calls # prints []<br>
&gt; print mock_reverse.mock_calls # prints []<br>
&gt; self.assertTrue(&#39;/natrium/cb/details/&#39; in response)<br>
&gt;<br>
<br>
</div></div>I might consider rewriting the test like this:<br>
<br>
import lib<br>
import mock<br>
<br>
@mock.patch.object(lib, &#39;reverse&#39;)<br>
def test_render_reverse(mock_reverse):<br>
    result = lib.render_reverse(&#39;foo&#39;, {&#39;args&#39;: [&#39;123&#39;]})<br>
    mock_reverse.assert_called_with(&#39;foo&#39;, args=&#39;123&#39;)<br>
    assert result == mock_reverse(&#39;foo&#39;, args=&#39;123&#39;)<br>
<br>
The assertion of the result might be pointless, but it feels like it confirms what the code under test should produce. Also, I realize I dropped the &quot;dummy_views&quot; from the argument simply b/c it seems superfluous in my example. Obviously, if it necessary, then it is necessary and should be in the test… right?<br>

<br>
If others have comments on my refactoring please let me know. I&#39;m new to the mock library and have been in the process of updating a rather old and unorganized test suite, so tips, tricks and best practices are all appreciated.<br>

<br>
Thanks!<br>
<br>
Eric<br>
<div class="im">&gt;<br>
&gt; Observations:<br>
&gt; 1. mock_reverse was never because `mock_calls` returns []. I&#39;ve asserted with `called` method, and it returns False.<br>
&gt; 2. using render_reverse like this, the name `dummy_views` must exists - it must be a view function that actually exists in my Django project. Otherwise, it will return an error.<br>
&gt; 3. using render_reverse like this, the speed takes 0.2 - 0.3 seconds, as opposed to 0.06 s when I used `mock_reverse`.<br>
&gt;<br>
&gt; Is there anything wrong the code? I understand that `mock_f` and `mock_kwargs` should be just simple input, rather than mock objects.<br>
&gt;<br>
&gt; Thanks, Carl.<br>
</div>&gt; _______________________________________________<br>
&gt; testing-in-python mailing list<br>
&gt; <a href="mailto:testing-in-python@lists.idyll.org">testing-in-python@lists.idyll.org</a> (mailto:<a href="mailto:testing-in-python@lists.idyll.org">testing-in-python@lists.idyll.org</a>)<br>
&gt; <a href="http://lists.idyll.org/listinfo/testing-in-python" target="_blank">http://lists.idyll.org/listinfo/testing-in-python</a><br>
<br>
<br>
<br>
</blockquote></div><br>