Mock framework comparison

time to read 4 min | 606 words

This is just a quicky, doesn't mean much, but allows a look at how each framework's syntax allows to setup a expectation & return value for a method:

NMock:

IMock mock = new DynamicMock(typeof(IRequest));
IRequest request = (IRequest)mock.MockInstance;
mock.ExpectAndReturn("Username","Ayende");
testedObject.HandleRequest(request);
mock.Verify();

EasyMock.Net:

MockControl control = MockControl.CreateControl(typeof(IRequest));
IRequest request = (IRequest)control.GetMock();
control.ExpectAndReturn(request.Username,"Ayende");
control.Replay();
testedObject.HandleRequest(mock);
control.Verify();

TypeMock.Net: [Update: This is the correct syntax] 

Mock requestMock = MockManager.Mock(typeof(ConcreteRequest));
requestMock.ExpectAndReturn("Username","Ayende");
//TypeMock.Net should take care of intercepting the calls for Username
testedObject.HandleRequest(new ConcreteRequest());
MockManager.Verify();

Rhino Mocks Version 1.0:

MockControl mockRequest = MockControl.CreateControl(typeof(IRequest));
IRequest request = (IRequest)mockRequest.MockInstance;
mockRequest.ExpectAndReturn(request.Username,"Ayende");
mockRequest.Replay();
testedObject.HandleRequest(request);
mockRequest.Verify();

NMock2:

using(Mockery mocks = new Mockery())
{
  IRequest request = (IRequest)mocks.NewMock(typeof(IRequest),"IRequest");
  Expect.Once.On(request).GetProperty("Username").Will(Return.Value("Ayende"));
  testedObject.HandleRequest(request);

Rhino Mocks Version 2.0:

using(MockRepository mocks = new MocksRepository)
{
   IRequest request = (IRequest)mocks.CreateMock(typeof(IRequest));
   Expect.On(request).Call(request.Username).Return("Ayende");
   mocks.ReplayAll();
   testedObject.HandleRequest(request);
}

Anyone wants to guess who get this editor's prize? :-)