Question

I know this is fairly subjective, but I'm diving into testing and learning about mocking, and I'm trying to figure which framework I should use. If you could please tell me which ones you recommed and most importantly why it's better than others you've used I would aprpeciate. Or if anyone knows where I can get a side-by-side comparison that would aslo be helpful.

Was it helpful?

Solution

Moq is the most advanced. It uses all features of .NET 3.5 and C# 3.0. This would be interesting:

OTHER TIPS

I've used Rhino.Mocks, Moq & NMock. I used to prefer Moq.

I now use NSubstitute... and found that its syntax was far superior to that of moq's and you don't sacrifice anything by way of power.

I used to write tests like this:

[Test]
public void SomeOtherTest()
{
    //Arrange
    var mock = new Mock<IFoo>();
    var sut = new SystemUnderTest(mock.Object); //never liked doing it this way...
    mock.Setup(m => x.Bar()).Returns("A whole bunch of ceremonial syntax..");
    //Act
    sut.DoSomething();
    //Assert
    mock.Verify(m => m.Baz()); //Baaaaah, more laaaaambdas
}

Now I revel in the non-lambda-eryness

[Test]
public void NSubTest()
{
    var mock = Substitute.For<IFoo>();
    var sut = new SystemUnderTest(mock); //much nicer!
    mock.Bar().Returns("Look ma! No lambdas!");

    sut.DoSomething();

    mock.Received().Baz();
}

Final point for.. its on github...

http://nsubstitute.github.com/

I like RhinoMocks, but it's the only one I've used :}

This looks promising: http://code.google.com/p/mocking-frameworks-compare/

I'm also using RhinoMocks. I particularly like the AAA (Arrange-Act-Assert) pattern. RhinoMocks makes it easy to set up expectations and check them using this pattern. The syntax uses lambdas and chaining, which fits in very well with LINQ. The similarity in syntax helps with understanding and allows the code to be more compact. the only issue I have with it, and it's not huge, is that in order to mock a method it needs to be virtual. In a sense this is good because it "forces" you to refactor to interfaces, but it can be a pain if an interface isn't really required. It can make mocking some framework classes more difficult. You can get around this by marking your class methods virtual or, with framework classes, creating a wrapper that you mock instead. I don't think these issues are unique to RhinoMocks.

You might find Richard Banks' series of posts comparing mocking frameworks useful.

Note: In the interest of full disclosure, I am co-author of NSubstitute which comes out pretty favourably in the comparison. :)

I've used NMock and think it's excellent. http://www.nmock.org/

However - this is the only one I've used.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top