views:

446

answers:

3

I'm using Visual Studio unit testing framework and have the need to using a mocking framework. And I'm not sure which of the mocking frameworks NMock2, Rhino mock that I should choose. Would you please share your experience? Thanks!

+1  A: 

I've been very happy with RhinoMocks using the AAA (Arrange-Act-Assert) pattern. I was able to get up to speed with it pretty quickly and am able to do almost all of what I need without resorting to hand mocks. Some of the other usage patterns available in RhinoMocks are less clear to me so I'd recommend using AAA.

   // Arrange

   var mock = MockRepository.GenerateMock<IMyInterface>();

   mock.Expect( m => m.Method() ).Return( value ).Repeat.AtLeastOnce();

   //  Act

   var testClass = new TestClass( mock ); // injection of mocked object

   var expected = ...expected result from method...
   var actual = testClass.TestMethod();

   // Assert

   Assert.AreEqual( expected, actual );

   mock.VerifyAllExpectations();
tvanfosson
+3  A: 

I would strongly consider Moq as well if you're working with C#3.5. Rhino documentation does not clearly differentiate between the number of "syntaxes" that you can use, which easily leads to mix-ups in writing your tests that can lead to all kinds of errors.

After introducing myself to Rhino for the last two months, and incorporating it into our tests for a new product version we are developing, I am in the process of changing everything over to Moq. I simply cannot introduce Rhino to our developers and get success with it, the learning curve is too time intensive for very little gain. I don't care if they understand the difference between stubs and mocks, partial or strict or full.

The simplicity and discoverability of the Moq API means all our developers can start mocking right away without caring about niggly differences. I guess it helps that our managers have the same philosophy on mocking that the creator of Moq does.

womp