Those of you that use RhinoMocks probably know how to do this already, however, I spent about twenty minutes getting this right.  I was writing a test that was looking to ensure that a call to a mocked object doesn’t happen.  As I created the test, it passed right away which was a bad sign, even though the mocked object was being called.

There are a couple of methods a person can use to create mock objects.  Be sure to use the right one for your test.

  • MockRepository.CreateMock – This creates a mock object where errors will be thrown for any method that is called and not recorded
  • MockRepository.DynamicMock – This creates a mock object where errors will not be thrown by unrecorded methods.


In brief, I first used the DynamicMock<T> method which of course swallowed the call and didn’t throw the error.  So after changing to CreateMock<T>, here is how my final test ended up:




A couple things to note:

Notice that the using for the record has no Expect calls


Technorati Tags: ,