One common use of a mocking framework is to check interaction between classes, Specifically checking correct arguments passing between calls.
Like any other framework Typemock Isolator also contain such an ability:
1 |
<span class="kwrd">using</span> (RecordExpectations recorder = RecorderManager.StartRecording())<br />{<br /> MyClass mock = <span class="kwrd">new</span> MyClass();<br /> mock.DoStuff(5);<br /> recorder.CheckArguments();<br />} |
However when doing so its important to note that the verification is done during the actual call time. When later on in the test when someone calls the DoStuff method and passes arguments they are checked immediately and the Isolator will fail the test by throwing an exception on the call.
this approach which is used by most framework can be confusing when happening inside a try-catch block.
for example the following code will not fail as expected:
1 |
<span class="kwrd">try</span><br />{<br /> MyClass target = <span class="kwrd">new</span> MyClass();<br /> target.DoStuff(4);<br />}<br /><span class="kwrd">catch</span><br />{<br /> <span class="rem">//some error handling code</span><br />} |
The exception thrown by the Isolator is caught, and in most cases not re-thrown and the test does not see it.
The trick to solve such an issue is to defer the argument checking to the final verification stage (the call to MockManager.Verify) by doing:
1 |
MockManager.ValidateArgsOnVerify = <span class="kwrd">true</span>; |
That being said we are working on removing the need for the user to specify this. We hope to incorporate this in our next release.