I want to start using mock objects on my next C# project.
After a quick Google search I've found there are many:
-
NMock
-
EasyMock.NET
-
TypeMock Isolator Commercial / Paid
-
Rhino Mocks
-
Moq
-
NSubstitute
-
JustMock Commercial / Paid
-
FakeItEasy
-
Microsoft Fakes (formerly Moles) Commercial (included with VS 11 Premium/Ultimate)
So my question is: what one is your favourite .NET mocking framework and why?
Source: Tips4all
I've not used most of the ones you've listed, so I can't be objective about it, but I use Moq and it has been awesome. The fluent interface makes it a joy to work with. For example:
ReplyDeletemockService.Expect(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>.
Roy Osherove has a poll about it on "Choosing a Mock Object Framework". Some frameworks that you have not listed:
ReplyDeleteNUnit.Mocks
NMock2
Top 3 frameworks from the poll are:
Rhino Mocks
TypeMock
NMock
Unfortunately, Moq is not among the choices. But maybe this post will help "Why do we need yet another NET mocking framework?" (this is about 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.
ReplyDelete// 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 http://code.google.com/p/fakeiteasy/
Another vote for Rhino Mocks here. My reasons are simple:
ReplyDeleteIt's free
It's open source
It's easy to use.
The syntax is great (logical and consistent).
I tried NMock and TypeMock and found both lacking.
I assume you have choosen your framework by now, but hopefully this post will help others to choose what's right for them!
ReplyDeleteI've used Moq and was mostly happy with that. The syntax makes tests pretty readable. However Roy Osherove introduced me to FakeItEasy, 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!
Are you on .NET 3.5? If you are then consider Moq, it is a full-featured mocking framework some some really nice features.
ReplyDeleteIf 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.
Rhino Mocks
ReplyDeleteOren is a genius
Open Source
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 by Roy Osherove
Have a look at NSubstitute. Also, here is a series of blog posts comparing some of the major players, including NSubstitute.
ReplyDeleteTypemock. 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).
ReplyDeleteThe only thing is it is not free for commercial development.
Edit: A bit of shameless plug, here's an article I wrote on unit testing ASP.NET MVC using Typemock AAA syntax.
I prefer Moq 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
ReplyDeleteI 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!
ReplyDeleteAn alternitive with intuitive and easy to learn syntax is http://simpledotnet.codeplex.com/
ReplyDeleteDisclaimer, I work for Typemock.
ReplyDeleteMr 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
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).
ReplyDeleteBesides that (and this is a bit off-topic) I will be using Ninject in the near future as the IoC 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.
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.
ReplyDeleteOn another interesting note it looks like we have a "NMock3" that's now in beta:
http://nmock3.codeplex.com/
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.
ReplyDeleteSeriously 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.
ReplyDeleteAnother 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.
ReplyDelete