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"><<a href="mailto:Ronny.Pfannschmidt@gmx.de" target="_blank">Ronny.Pfannschmidt@gmx.de</a>></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>
>>> m = mock.Mock()<br>
>>> m.method.x.return_value = 1<br>
>>> m.method().x.return_value = 2<br>
>>> m.method().x<br>
<Mock name='mock.method().x' id='24232272'><br>
>>> m.method().x()<br>
2<br>
>>> 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 {'result': 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't Repeat Yourself)<br>
self.p1 = patch('pyramid_app.views.<u></u>MyModel')<br>
self.mk1 = self.p1.start()<br>
self.mk1.query.first.return_<u></u>value = 'abc'<br>
<br>
<br>
def test(self):<br>
r = my_view(Request)<br>
self.assertEqual({'result': 'abc'}, r)<br>
<br>
<br>
But the actual return is:<br>
<br>
AssertionError: { 'result': 'abc'} != {'result': <MagicMock<br>
name='DBSession.query().first(<u></u>)' id='185999084'>}<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>