Mock object bug with assert_not_called (Steve D'Aprano)

Diego Vela diego at vida.com
Wed Dec 28 18:36:05 EST 2016


Re: mock bug.

Python 2.7.8


I'll provide a sketch of the code since it is from work and I'm not allowed
to share it directly.

Sketch of Code:
@patch('emails.emails.render_to_string')
def test_send_email(self, render):
    context = {
          'key': value
    }
    emails.send_email() # calls render_to_string
    render.assert_any_call(
         'template.txt',
         context
     )
    render.assert_not_called()


Expected behavior is that one of the asserts should raise an exception.
The assert_not_called() should assert that the mocked method was not called
at all.  The assert_any_call(vars) asserts there is at least one call with
the given args.  If neither throws an exception then this is a
contradiction because the mocked object, render in this case, has asserted
that there are no calls and that there is at least one call.

Is this sufficient?  I'm not so sure, this is my first time.  Thanks for
the help.

-- 
Diego Vela



More information about the Python-list mailing list