Thanks.<br>I made a mistake when copying the code. I was mocking `DBSession` instead of `MyModel`. But like your example showed, when I called  self.mk.query().first.return_value it will give the return value. I am guessing this is because queyr is a callable, and making self.mk.query.first is like accessing a non-callable attribute. <br>
<br>Thank you.<br>John<br><br><div class="gmail_quote">On Fri, Aug 3, 2012 at 2:09 AM, Ronny Pfannschmidt <span dir="ltr">&lt;<a href="mailto:Ronny.Pfannschmidt@gmx.de" target="_blank">Ronny.Pfannschmidt@gmx.de</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi John,<br>
<br>
you are mocking the first attribute of the query method<br>
you want to mock the first attribute of a call result of the query method<br>
<br>
&gt;&gt;&gt; m = mock.Mock()<br>
&gt;&gt;&gt; m.method.x.return_value = 1<br>
&gt;&gt;&gt; m.method().x.return_value = 2<br>
&gt;&gt;&gt; m.method().x<br>
&lt;Mock name=&#39;mock.method().x&#39; id=&#39;24232272&#39;&gt;<br>
&gt;&gt;&gt; m.method().x()<br>
2<br>
&gt;&gt;&gt; m.method.x()<br>
1<br>
<br>
<br>
-- Ronny<div><div class="h5"><br>
<br>
On 08/03/2012 07:20 AM, John Wong wrote:<br>
</div></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div class="h5">
Suppose I have this views function:<br>
<br>
     res = DBSession.query(MyModel).<u></u>first()<br>
         return {&#39;result&#39;: res}<br>
<br>
I want to use mock to write my unittest<br>
<br>
     def setUp(self):<br>
         # I am setting up a global patch (Don&#39;t Repeat Yourself)<br>
         self.p1 = patch(&#39;pyramid_app.views.<u></u>MyModel&#39;)<br>
         self.mk1 = self.p1.start()<br>
         self.mk1.query.first.return_<u></u>value = &#39;abc&#39;<br>
<br>
<br>
     def test(self):<br>
         r = my_view(Request)<br>
         self.assertEqual({&#39;result&#39;: &#39;abc&#39;}, r)<br>
<br>
<br>
But the actual return is:<br>
<br>
     AssertionError: { &#39;result&#39;: &#39;abc&#39;} != {&#39;result&#39;: &lt;MagicMock<br>
name=&#39;DBSession.query().first(<u></u>)&#39; id=&#39;185999084&#39;&gt;}<br>
<br>
But I already set the return_value on the mock object, why do I still<br>
get the instance of the MagicMock and not the return_value?<br>
I can, of course, access the `return_value`, but that is not a solution<br>
at all.<br>
<br>
<br>
Thanks.<br>
<br>
John<br>
<br>
<br></div></div>
______________________________<u></u>_________________<br>
testing-in-python mailing list<br>
<a href="mailto:testing-in-python@lists.idyll.org" target="_blank">testing-in-python@lists.idyll.<u></u>org</a><br>
<a href="http://lists.idyll.org/listinfo/testing-in-python" target="_blank">http://lists.idyll.org/<u></u>listinfo/testing-in-python</a><br>
</blockquote>
<br>
</blockquote></div><br>