Rhino Mocks: Defaulting CreateMock vs DynamicMock

Over the past year of using Rhino Mocks I've adopted the following pattern..

public void Setup()
  _mockery = new MockRepository();
  _mockView = _mockery.CreateMock<IMyView>();
  _mockServiceLayer = _mockery.CreateMock<IMyServiceLayer>();
  // etc..

private IMyClass CreateSUT()
  return new MyClass(_mockView, _mockServiceLayer);

The reason I used CreateMock as my default was as a fail safe measure.  If during a test something was unexpectedly called I would get an expectation exception and know I had missed something.  Of course during most of this time I was really bad at over specifying my tests and I think the way I was defaulting helped lead to that.

Recently I started following the "one mock per test" guideline but I didn't switch my use of CreateMock.  Now I'm starting to notice that some of my tests have a small block at the start which just converts mocks over to DynamicMock.  So now I've got polluted tests and I'm incurring the cost of creating a mock twice.

I'm also more on the fence now as to the value of CreateMock catching missing expectations.  I honestly couldn't recall a time that has been a help.  So now I'm starting to think that maybe I should be using DynamicMock as the default for my tests and switching the important mock in a test to CreateMock as part of that test.

So I'm hoping someone has either done this or has an even better idea of how to clean up my tests...

posted @ Tuesday, November 20, 2007 8:51 PM

Comments have been closed on this topic.