What should I consider when choosing a mocking framework for .Net [closed]
Asked Answered
D

8

32

There are lots of mocking frameworks out there for .Net. There is no clear winner that has superseded the others in every way. The leading mocking frameworks also have many different styles of usage.

The time it takes to learn all of the mocking frameworks well enough to decide which to use is unreasonable. I don’t believe that we have yet reached a stage that we can talk about the best mocking framework. So what questions should I be asking, both about the project, and about myself, to help decide on the best mocking framework to use, in a given case?

It would also be useful to know why you chose the mocking framework you are currently using and if you are still happy with that choice.

Is there a useful vocabulary to use when comparing the styles of mocking frameworks?

(Note: I have limited this question to .Net as Java does not have attributes or lambda expression, so I hope the mocking frameworks can be better for .Net then Java)

Summary so far:

  • If you need to mock static method, or none virtual methods then the only reasonable option is TypeMock, however it is not free and does not drive you towards a good design.
  • Rhino Mocks is a very good option if you are doing TDD, .e.g the objects you wish to mock implement interfaces. At present it seems to be the "market leader"
  • Moq (introduction) should be considered if you are using .NET 3.5 Moq may be gaining on Rhino Mocks for new projects

What have I missed from this summary?

So what drives the choice between Rhino Mocks and Moq, if you are using .NET 3.5?


see also:

What should I consider when choosing a dependency injection framework for .NET?” may also be of interest as it asks the “other side” of the question.

Despotic answered 13/3, 2009 at 13:11 Comment(1)
Anyone tried JustMock telerik.com/products/mocking.aspx from telerik?Sofiasofie
V
21

So what questions should I by asking about the project and myself to help decide on the best mocking framework to use in a given case?

The questions you should be asking about the project is: Has the project been developed with the SOLID principles, or not? Is this a project that has loose coupling and high cohesion? Have good OO principles been utilized in building the project? Is a Dependency Injection container being utilized? Has the system been coded in a Design by Contract method (utilizing Interfaces thoroughly)?

If you answer yes to these questions, then you can utilize a mocking framework like RhinoMocks, which is what some would call an "opinionated" framework. RhinoMocks, and some other mocking frameworks, have very strong opinions about how a system should be designed in order for objects to be mocked. A framework like RhinoMocks expects your project to be designed a certain way. Mocking is certainly a lot easier with RhinoMocks when you've built your code the right way (no sealed classes, no statics, heavy use of interfaces, virtual on class methods, etc.)

If you answer no to those questions, or if you're working on a legacy system with a lot of highly coupled classes, then your only choice is going to be TypeMock, which can mock just about anything.

It would also be useful to know why you choose the mocking framework you are currently using and if you are still happy with that choose.

I chose RhinoMocks because at the time (3+ years ago) it was clearly the most mature mocking framework with the most features. I've stayed with it because it has evolved in away that makes my life much easier (the advent of the AutoMocking container being a gigantic step toward efficiency).

What I like about RhinoMocks, other than the feature set and ease of use, is that it guides me toward a better design in my code. I am not a perfect programmer, and I am going to make mistakes in design. But tools like RhinoMocks and NHibernate help guide me toward a better design, because when I do make mistakes and create poor design, these tools become painful to work with. NHibernate, for instance, is painful to work with if you have a bad database design. RhinoMocks is very painful to work with if you have a poor class design, aren't using interfaces, aren't using IoC... etc.

I like RhinoMocks because it ultimately helps me be a better developer, and not just because I'm testing my code, but because I'm shaping my code - designing it - in a better manner.

Valma answered 13/3, 2009 at 14:42 Comment(2)
A nice answer and true in a lot of cases but it's important to remember that technically good code doesn't necessarily map to the constraints of the business. Great design is wonderful but can be time consuming,..sometimes there just isn't the time to be so robust yet you still need a mocking framework to be flexible enough to allow you to get the job done.Goolsby
Unfortunately, I can't find any evidence to support an statement like "it guides me toward a better design in my code". On the contrary, my experience tells me it guides you towards an over-engineered design, and often prevents good design.Senhorita
C
14

I prefer Moq for .NET 3.5 projects. It is simple to use and, in my experience, it helps to produce clean and clear unit tests.

There's no technical reason why you can't use more than one mocking framework on the same project. Sure, it's nice to standardize, but some tests may lend themselves better to some frameworks.

Courses answered 13/3, 2009 at 14:4 Comment(0)
L
10

Disclaimer – I work for Typemock

I disagree with "Typemock does not drive you to good design". It is you, the developer that drives a good or bad design, not the tool that you use. Get the tool that gets the job done, and make you productive, but the responsibility of good design is your own. If you think that wrapping all kind of abstractions inside your code, just for testing, go for it, but you may not come up with a "good" design. It could be more complex than the design you have right now.

Lectern answered 21/4, 2009 at 16:2 Comment(0)
T
9

What should I consider when choosing a mocking framework for .Net

Another aspect to consider, besides the design of the code base that you are testing, is how readable your tests are when using mocks.

In this regard, I am putting in a vote for NSubstitute - one of the newer mocking frameworks with a very expressive API (no lambdas) and comparable functionality with Moq and Rhino Mocks. It has recently reached version 1.0.

A usage example (from the NSubstitute homepage):

//Create:
var calculator = Substitute.For<ICalculator>();

//Set a return value:
calculator.Add(1, 2).Returns(3);
Assert.AreEqual(3, calculator.Add(1, 2));

//Check received calls:
calculator.Received().Add(1, Arg.Any<int>());
calculator.DidNotReceive().Add(2, 2);

//Raise events
calculator.PoweringUp += Raise.Event();

A detailed comparison of the readability of tests using RhinoMocks, Moq and NSubstitute is given here.

Tahiti answered 18/2, 2011 at 8:22 Comment(0)
D
3

stubs looks interesting and I think it may ship with .NET V4. I don’t know witch edition of developer studio you will need to use it. At present you can download it from the above link.

Stubs is a lightweight framework for test stubs and detours in .NET that is enterily based on delegates, type safe, refactorable and source code generated. Stubs was designed support the Code Contracts runtime writter and provide a minimal overhead to the Pex white box analysis. Stubs may be used on any .NET method, including non-virtual/static methods in sealed types.

(I have posted this as an answser rathern then adding it to the quesion, as I have never used Stabs and only spend a few minutes looking at it)

Despotic answered 5/11, 2009 at 10:17 Comment(0)
S
2

RhinoMock is pretty much the state-of-the art mocking framework for .NET. Can't go wrong with it. I guess you can view its "style" as a "Jack of all trades" if you so wish.

From their web site:

What does Rhino Mocks offer?

  • Explicit record & replay model for expectations.
  • Natural Arrange, Act, Assert syntax
  • Support for .Net 2.0 and .Net 3.5
  • Working with strongly typed mocks.
  • Setting actions on methods, return spesific value, or throw an exception.
  • Expectations based on:
    • Arguments matching
    • Constraints matching
    • Custom callback to verify the expected arguments using your own code
Sadiesadira answered 13/3, 2009 at 13:12 Comment(0)
M
0

I would recommend FakeItEasy. It's a really descriptive mocking framework that allows very easy reading and mocking of interfaces. It has an active open source community and works very well.

Mcspadden answered 3/5, 2012 at 10:37 Comment(0)
B
0

I use Telerik JustMock, it is very professional and easy mock framework with a good document.

Bolero answered 3/6, 2012 at 18:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.