I want to start using mock objects [1] on my next C# project.
After a quick Google search I've found there are many:
So my question is: what one is your favourite .NET mocking framework and why?
I've not used most of the ones you've listed, so I can't be objective about it, but I use Moq [1] and it has been awesome. The fluent interface makes it a joy to work with. For example:
mockService.Setup(s => s.GetCustomers()).Returns(new List<Customer>());
@Ngu Soon Hui, I wasn't aware that the other frameworks don't have compile-time checking. Moq certainly does. In my example above, if the service class that mockService is mocking doesn't have a GetCustomers() method, I would get a compile-time error. I'd also get one if the GetCustomers() method didn't return a List<Customer> or an interface like IList<Customer>.
[1] http://code.google.com/p/moq/I used Rhino.Mocks before and it's certainly a good product. However there were some painpoints for me and I don't like the Moq-style where you have "Mock-object". To scratch this itch I created my own "fake-framework" called FakeItEasy. Now almost a year later it's a pretty mature project that's used in large scale production systems.
// Creating a fake object is just dead easy!
// No mocks, no stubs, everything's a fake!
var lollipop = A.Fake<ICandy>();
var shop = A.Fake<ICandyShop>();
// To set up a call to return a value is also simple:
A.CallTo(() => shop.GetTopSellingCandy()).Returns(lollipop);
// Use your fake as you would an actual instance of the faked type.
var developer = new SweetTooth();
developer.BuyTastiestCandy(shop);
// Asserting uses the exact same syntax as when configuring calls,
// no need to teach yourself another syntax.
A.CallTo(() => shop.BuyCandy(lollipop)).MustHaveHappened();
Check it out at fakeiteasy.github.io [1]
[1] http://fakeiteasy.github.io/Roy Osherove has a poll about it on " Choosing a Mock Object Framework [1]". Some frameworks that you have not listed:
Top 3 frameworks from the poll are:
Unfortunately, Moq is not among the choices. But maybe this post will help " Why do we need yet another NET mocking framework? [4]" (this is about Moq)
[1] http://www.osherove.com/blog/2007/4/26/choosing-a-mock-object-framework.htmlNUnit.Mocks
framework is a "toy" - groups.google.com/group/nunit-discuss/msg/55f5e59094e536dc Rude awakening to some: "I'm beginning to think that [NUnit Mocks] was a mistake, because you are far from the first person to become reliant on it." - John K
I assume you have choosen your framework by now, but hopefully this post will help others to choose what's right for them!
I've used Moq and was mostly happy with that. The syntax makes tests pretty readable. However Roy Osherove introduced me to FakeItEasy [1], and I find that even more fluent and easy to use than Moq.
You should choose a mocking framework based on your needs. If you are planning to do your system from scratch, you should design it for testing and then one of the free frameworks would be more than sufficient! I can't come up with any reason for myself changing to a commercial mocking framework.
I would however like to mention that there are some very important differences between the mocking frameworks. Some are proxy based, like Moq, FakeItEasy and Rhino Mocks. Your fakes will be created on the fly, implementing or overriding the classes/interfaces. You have to stick to the rules of inheritance, so you can't just hook into a method that doesn't support overriding or are defined in an interface. They kind of force you to create testable code.
Justmock and Typemock are profiler based, meaning that they can intercept calls to any method to assert that it has been called, changing its return value, etc. If you, for example, have a legacy system or you depend on another commercial system like SharePoint and want to write tests, one of the commercial frameworks might do the trick.. Say you have dependencies to static classes like DateTime.Now
, Random
or something home grown. These frameworks will make it possible for you to intercept the calls and return your own values! If you are using a proxy based framework, you must design/refactor your code to get past such dependencies (for example, extract and override).
Good luck!
[1] http://code.google.com/p/fakeiteasy/Have a look at NSubstitute [1]. Also, here is a series of blog posts [2] comparing some of the major players, including NSubstitute.
[1] http://nsubstitute.github.com/Another vote for Rhino Mocks here. My reasons are simple:
I tried NMock and TypeMock and found both lacking.
Are you on .NET 3.5? If you are then consider Moq [1], it is a full-featured mocking framework some some really nice features.
If not on .NET 3.5 then I would go with Rhino Mocks. They have a huge community following so the answers to your questions should be easily available.
[1] http://code.google.com/p/moq/Rhino Mocks
If you need to test a ton of legacy code, you might look into TypeMock as it can mock just about anything known to man ;)
For more information, checkout this post [1] by Roy Osherove
[1] http://weblogs.asp.net/rosherove/archive/2007/04/26/choosing-a-mock-object-framework.aspxI just discovered NSubstitute, and for a rusty old programmer like myself, NSubstitute is a total God-send! After messing about with Rhino Mocks for ages, I kept on hitting all sorts of obscure roadblocks, like being unable to mock .ToString(), but NSubstitute just works. Plug and play, buddy. Hooray for well-designed software!
Typemock [1]. It's the only mocking framework that allows you to check your mocking calls in compile time ( You can use natural mock for that purpose).
The only thing is it is not free for commercial development.
Edit: A bit of shameless plug, here's an article [2] I wrote on unit testing ASP.NET MVC using Typemock AAA syntax.
[1] http://www.typemock.comI prefer Moq [1] when I develop with .NET 3.5. Very nice use of the lambda expressions. Otherwise I think I'll use RhinoMocks on a .NET 2.0 only project
[1] http://code.google.com/p/moq/Disclaimer, I work for Typemock.
Mr Rogers – A good resource for ASP.NET unit testing (that doesn't use Rhino.) As an example: http://www.typemock.com/ASP.NET_unit_testing_page.php
Another alternitive is JustMock from Telerik. They have free and commercial versions. I was using free version for one of my projects and very happy with the results - quick and easy.
An alternitive with intuitive and easy to learn syntax is http://simpledotnet.codeplex.com/
I'm in charge of deciding on which mocking framework to use in my company. I've used both Moq and Rhino Mocks and so far I'm digging Moq. Some of our developers get confused by lambda expressions, but I think once you get used to them Moq is awesome. Rhino "seems" a little bit more powerful so far, but I prefer the fluent interface on Moq. I also think the record type syntax would confuse everyone more than the more concise lambda expressions.
On another interesting note it looks like we have a "NMock3" that's now in beta: http://nmock3.codeplex.com/
I'm currently unit testing with Moq, and I must say it works pretty well! - rephrase: It works great! I've not used any of the other mocking frameworks you mention, so I can't give you a comparison. But I can say that I'm glad that I've chosen Moq as my first-to-try mocking framework. The lamda expressions are really nice and it's also pretty lightweight and reader friendly (the record/replay syntax in most other mocking frameworks aren't really doing your readability any good).
Besides that (and this is a bit off-topic) I will be using Ninject [1] in the near future as the IoC [2] container, and both frameworks go hand-in-hand. Ninject also has lambdas and it even provides auto-mocking container support for Moq (using an extension). So if you're also planning to use an IoC container you could check this awesome combination.
[1] http://ninject.org/Seriously consider looking at "Moles", Microsoft's offering for mocking. It has by far the best syntax (mocking a method is simply supplying a delegate to a stub class, much nicer that other frameworks). It also supports mocking static methods, sealed classes, constructors, etc., without having to change any source code whatsoever (i.e. you don't need to program to an interface to mock out objects and methods). It can mock any object, including objects in the .NET framework. Imaging mocking DateTime.Now without have pre-planned in your code to do so. And it is free.
I've had the same question as Corin because I've wanted to get more into test driven development. It seems like most of the examples for ASP.NET MVC that I found had Rhino Mocks examples. However, most of my test driven development has been with Ruby on Rails and Moq has really appealed to me. I love the simplicity and forward thinking design. It's definitely one I plan on trying.
I test many Mock frameworks, i found Telerik JustMock is very good and easy, it is a very professional and full-featured mocking framework, and also with a comprehensive document.
I have found Simple.Mocking [1] to be an easy framework to use (easy to fake interfaces, delegates, etc.). It's free and open source.
[1] http://simplemocking.codeplex.com/
myMock.Verify(foo => foo.MyMethod(It.IsAny<string>(), It.IsAny<int>(), Times.AtLeastOnce())
for a methodMyMethod(string, int)
. - Wernight