Suppose I have this views function:<br><br> res = DBSession.query(MyModel).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.MyModel')<br> self.mk1 = self.p1.start()<br> self.mk1.query.first.return_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 name='DBSession.query().first()' id='185999084'>}<br>
<br>But I already set the return_value on the mock object, why do I still 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 at all.<br>
<br><br>Thanks.<br><br>John<br>