Spying on constructor without mocking it

I’m trying to test some code which constructs an email.mime.text.MIMEText object, and I need to ensure that the proper text is passed to the _text argument when the constructor is called.

I can mock the constructor, like this:

(flexmock(email.mime.text.MIMEText)          .should_receive('__init__')          .with_args('message text', _charset='utf-8')) 

This works, in the sense that if the constructor is not called with the proper arguments then the test will fail because the expectation will fail. However, since __init__ is now a stub function, the returned object is not fully-formed, and the code which uses it will have random failures because its attributes have not been set properly.

The only workaround I’ve found so far is to mock all of the other methods that I invoke on the object as well, so that they are not dependent on the object being fully-formed, but this falls into the area of ‘testing too much’.

Is there a mechanism that could be used to allow the __init__ method to do all of its normal work, after checking the expectation against its arguments? I did try using should_call but that resulted in expectations being matched against the super-class __init___ calls too, which won’t have the same signature.

Add Comment
0 Answer(s)

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.