Tim Van Wassenhove

Passionate geek, interested in Technology. Proud father of two

23 Mar 2009

Comparing Moq to Rhino Mocks

So which mocking framework should we use? Do we fall back on good old Rhino Mocks or do we choose for the new kid on the block Moq?

From a technical point of view i would dare to say that they will be able to support the same set of features because they’re both based on Castle’s DynamicProxy. Rhino Mocks has the advantages that it, unlike Moq, supports the mocking of Delegates and can be used in a .Net 2.0 only environment. Whether or not we should care about these differences is a question i will leave unanswered.

From a user point of view i find the Moq API a bit cleaner because it frees me from thinking about the record-replay model and Rhino Mocks wants me to make assertions on a stub. Apart from that the differences are rather small given for the use-cases i presented in Getting started with Moq.

  • Creating instances
// Rhino  
var productDetailView = MockRepository.GenerateStub<iproductDetailView>();

vs

// Moq
var productDetailViewMock = new Mock<iproductDetailView>();
  • Setting up results
// Rhino Mocks
productRepository
    .Stub(repository => repository.GetCategory(0)).IgnoreArguments()
    .Do((Delegates.Function<icategory, int>)(categoryId => return category;));

vs

// Moq
productRepositoryMock    
    .Setup(repository => repository.GetCategory(It.IsAny<int>())) 
    .Returns<int>(categoryId => return category.Object;});
  • Consuming the mocked objects
// Rhino Mocks
// Nothing to do

vs

// Moq  
// Nothing to do
  • Raising events
// Rhino Mocks
productDetailView.Raise(view => view.EditClick += null).Raise(sender, EventArgs.Empty);

vs

// Moq  
productDetailViewMock.Raise(view => view.EditClick += null, sender, EventArgs.Empty);
  • Verifying that a method was invoked
// Rhino Mocks -- Odd that i'm asserting on a Stub.
productRepository.AssertWasCalled(    
    repository => repository.FindCategories(
    Arg<ispecification<icategory>>.Matches(Is.TypeOf(typeof(All<icategory>)))));

vs

// Moq
productRepositoryMock.Verify(    
    repository => repository.FindCategories(
		It.Is<ispecification<icategory>>(specification => specification.GetType() == typeof(All<icategory>))));
  • Override a result
// Rhino Mocks  
productDetailView.BackToRecord();
    
productDetailView    
    .Stub(view => view.CategoryId)
    .Returns(2);

vs

// Moq  
productDetailViewMock
    .Setup(view => view.CategoryId).Returns(2);

Editted code to take advantage of the new AAA syntax that comes with Rhino Mocks 3.5 If only someone could show me how i can override a Stubbed result…