views:

11456

answers:

16

I want to start using mock objects on my next C# project.

After a quick google I've found there are many:

So my question is: what one is your favourite .NET mocking framework and why?

+61  A: 

I've not used any 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, C# 3.0 interface makes it a joy to work with. For example:

mockService.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>.

Matt Hamilton
C# 3.0, nemo, which means .NET 3.5 or above.
Matt Hamilton
I dislike Moq cause everything is a Mock there. Mock is supposed to be one per test.
Arnis L.
Huh? "Everything is a Mock"? I mock only the things that support the object I'm testing, nothing more.
Matt Hamilton
Another vote for Moq. Fluent interface, pretty extensive compile-time checking, intellisense Just Works; it's great.I strongly suggest you use the latest trunk version; the binaries on the website were missing some fixes that I really needed, like support for larger argument counts among other things.One big Moq downside is the docs lag behind the implementation, but if you invest the time to learn how it actually works, it pays dividends almost immediately.
anelson
@Arnis L. - "Mock is supposed to be one per test" - what do you mean by this? If I am testing an object which has two external dependencies which I need to mock out, what am I supposed to do? In this case I clearly need two mocked objects.
AdamRalph
@AdamRalph that's what Osherov wrote in his 'Art of unit testing' book. Mock is a fake instance that we verify against where stub is just a fake instance that helps to get things up and running (mock kind a inherits stub, is a bit advanced). In your case - your external dependencies likely would be just a stubs. Moq framework blurs this line and there's just a Mock and no stubs. Lack of dividing responsibilities where i believe it shouldn't be taken away. That's all. :)
Arnis L.
One thing I has some trouble finding it checking that a method is called with someagument (whataver arguments). It's done like this: `myMock.Verify(foo => foo.MyMethod(It.IsAny<string>(), It.IsAny<int>(), Times.AtLeastOnce())` for a method `MyMethod(string, int)`.
Wernight
+7  A: 

Rhino Mocks

  • Oren 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

Toran Billups
You should also check if Moles from Microsoft can help you if you have legacy code.
Piotr Owsiak
+6  A: 

Tyemock. 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 I wrote on unit testing ASP.NET MVC using Typemock AAA syntax.

Ngu Soon Hui
Wrong. Moq supports compile time checking.
Finglas
+18  A: 

Roy Osherove has a poll about it on "Choosing a Mock Object Framework". Some frameworks that you have not listed:

Top 3 frameworks from the poll are:

  1. Rhino Mocks
  2. TypeMock
  3. 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)

John
+13  A: 

Another vote for Rhino Mocks here. My reasons are simple:

  1. It's free
  2. It's open source
  3. It's easy to use.
  4. The syntax is great (logical and consistent).

I tried NMock and TypeMock and found both lacking.

akmad
[Personal Opinion] It's also a bit too much to get started... I checked it out.. i guess last year and found the learning curve steep and the syntax non-intuitive.
Gishu
Agreed. I'm looking at switching to Moq. There is no way I can get buy-in from all 10 of my developers if they have to go through what I've gone through to even scratch the surface of Rhino.
womp
Rhino Mocks is good, but ... logical and consistent? It's evolved to the point where there's sometimes 3 ways to do any one thing! I like it a lot, but there's a lot of room for improvement in the API.
Mark Simpson
I can't comment on the different mocking styles that have been added, I just use the standard style and find it sufficient (as well as logical and consistent ;)) for all my needs. However, with the additional styles that have been added I can see your point.That said, if I were just learning mocking in .Net I would go with Moq.
akmad
A: 

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.

Mr Rogers
+9  A: 

Are you on .NET 3.5? If you are then consider Moq, 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.

Rob Bazinet
A: 

Typemock is a more professional mocking framework, recommended for companies and serious development.

Rhino on the other hand, is free... good for beginners and smaller projects.

Typemock has a product for open source projects that is free
Dror Helper
Just because something doesn't cost a ton of money doesn't mean it's not professional or serious! FAIL
Orion Edwards
A: 

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

+3  A: 

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

Michaël Larouche
+1  A: 

An alternitive with intuitive and easy to learn syntax is http://simpledotnet.codeplex.com/

mikwal
A: 

I'm a little late to the party. I'm in charge of deciding on which mocking framework my company uses. I've used both Moq and Rhino Mocks and so far i'm digging Moq. Some of our developers get confused by lamda 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" thats now in beta: http://nmock3.codeplex.com/

A: 

[Old topic, but might still be usefull to others]:

I'm currently Unit Testing in 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 in the near future as IoC Container, and both frameworks go hand-in-hand. Ninject also has Lamda's 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 :]

Bas
+4  A: 

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 http://code.google.com/p/fakeiteasy/

Patrik Hägne
+4  A: 

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 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 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 RhinoMocks. 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 it's return value etc. If you e.g. 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 (e.g. extract and override).

Good luck!

HaraldV
+3  A: 

Have a look at NSubstitute. Also, here is a series of blog posts comparing some of the major players, including NSubstitute.

CAD bloke