Replacing Rhino with Moq
November 10th 2009Were just starting down the path of serious and extensive testing with mocks and fakes. Our initial tests were all done using Rhino Mocks, mostly because it had a well established reputation and we had no reason not to give it a try. The syntax is beautiful and did everything we needed, until we tried to make a test similar to this one pass successfully.
public interface IFoo
{
string Bar();
}
[Fact] public void TestWithRhino()
{
var mock = MockRepository.GenerateStub<IFoo>();
mock.Stub(x => x.Bar()).Return("bar");
mock.Stub(x => x.Bar()).Return("baz");
Assert.Equal("baz",mock.Bar());
}
We couldnt, at least we couldnt using strictly stubs. From what I could gather, we could if we used a more complicated for of mocking besides stubs. We only need stubs, or fakes, because we dont need to test all the interaction, just the ending state.
Since we dont have any serious stock in using Rhino, as an alternative I decided to take a look at using Moq. The syntax for Moq is not as nice. It is more wordy, but I do like how it doesnt rely on extensions methods for everything. But what about the sample test?
[Fact] public void TestWithMoq()
{
var mock = new Mock<IFoo>();
mock.Setup(x => x.Bar()).Returns("bar");
mock.Setup(x => x.Bar()).Returns("baz");
Assert.Equal("baz", mock.Object.Bar());
}
It passes! Switching wasnt that difficult, but where is the ugly syntax Ive run into so far? Well this in Rhino:
MockOrdersRepository.AssertWasNotCalled(x=>x.StartOrder(null,null,null,null));
...becomes this in Moq:
MockOrdersRepository
.Verify(x=>x.StartOrder( It.IsAny<string>(),
It.IsAny<string>(),
It.IsAny<string>(),
It.IsAny<string>()),
Times.Never());
It might be I am not familiar enough with either to say that is the equivalent assertion of both, but it is working for us. I am probably missing something that is causing Moq to look more verbose.