Home > Tech > Mock Framework comparison

Mock Framework comparison

Thursday, July 14, 2011 Leave a comment Go to comments

(originally from http://codevanced.net/post/Mocking-frameworks-comparison.aspx)

About 9 months ago I posted a comparison of different mocking frameworks. A lot has changed since then – armloads of bugfixes, gratifying influx of new features, and even a new mocking framework (!) – so time is ripe for a new comparison.

But before I move on, here’s a very brief list of changes.

Changes since January 2009.

1.NMock2 caught up with its major rivals, and now supports

· nonstrict mocks – it’s now perfectly ok to "unexpectedly" call a method on a mock

· (wow!) mocking classes

· partial mocks (that is, mocking only part of a type while leaving some methods unmocked)

2. Isolator made a huge leap forward – now you can

· mock mscorlib types – yes, mocking DateTime.Now is no longer an issue!

· completely redefine mocked method with DoInstead

· test ASP.NET with Ivonna and test SharePoint with um, Isolator for SharePoint

· easily write tests in VB

3. Rhino Mocks hasn’t changed much but

· it now officially supports recursive mocks (mocking chains like user.Details.Address.ZipCode in one line)

· there’s RM 4.0 in the pipeline and this one will be a bomb – Ayende plans to break backward compatibility and simplify everything

4. Moq hasn’t changed much either

· apart from introducing linq to moq which allows you to set up multiple mocks (with multiple conditions, etc) in one operator

5. And now we have Moles, dark horse of mocking

· that supports mocking static methods

· …and non-virtual methods

· …and even members of sealed classes (!!!)

Current comparison.

The comparison table follows. As all frameworks now support both strict and non-strict mocks, and can mock both classes and interfaces – I’ll omit that from "Pros".

Pros Cons Comments
NMock2
Open source and free.

Expressive and easy-to-learn syntax.

Clear internal errors (supports custom messages as well).

Supports partial mocks.

Very fast.

Heavily relies on Dependency Injection pattern.

Type unsafe: expectations are string based.

Cannot mock non-virtual, non-abstract and static methods, cannot mock sealed classes and private interfaces.

Does not support recursive mocks [Update: or maybe it does - see comments].

With extension methods of C#3, one can easily extend NMock2 to be type safe, at least for simple scenarios.

String based expectations is a deliberate choice – the team believes it gives you more power – and to make our lives easier, they made some terrific contribution. They wrote an open source Resharper plugin to help us writing all those strings.

One consequence of using string based expectations is that NMock2 is very fast as it doesn’t parse expression trees at runtime.

Rhino Mocks
Open sourse and free.

Type safe.

Advanced features: supports recursive mocks and partial mocks.

Heavily relies on Dependency Injection pattern.

Cannot mock non-virtual, non-abstract and static methods, cannot mock sealed classes and private interfaces.

Poor intellisense experience, all RM methods just appear in any call to any object in a test.

Intellisense is polluted because RM returns the actual <T> from its GenerateMock and other calls, but in order to allow calling Expect, Stub and other methods, RM does so by introducing extension methods on any T.

A lot will be changed with RM 4.0 which will break backwards compatibility and presumably make the API much easier to use.

Moq
Open source and free.

Expressive and easy-to-learn syntax.

Type safe.

Advanced features: supports recursive mocks, partial mocks.

Heavily relies on Dependency Injection pattern.

Cannot mock non-virtual, non-abstract and static methods, cannot mock sealed classes and private interfaces.

Need to use mock.Object property if we pass the mocked interface/class over.

Moq is the youngest framework, bereft of the "legacy" burden of record/replay model. This makes it pretty straightforward and friendly, and a lot of work is being done to support and enhance the clear API.

In order to keep clean intellisense, Moq separates between mocked objects and expectation builder objects – that’s why you need this Object property on a mocked instance.

Isolator
Does not rely on Dependency Injection pattern.

Expressive and easy-to-learn syntax.

Type safe.

Advanced features: supports recursive mocks, partial mocks.

Überadvanced features: can mock static methods, sealed and private classes.

Not open source and not free.

Does not support custom error messages in expectations.

Is the slowest mocking framework.

Isolator is the most powerful mocking framework, and sometimes its power is (unfairly?) considered as its main weakness.

Mocking static methods etc is possible because it uses CLR profiler to intercept the calls. And even though it seemed not possible, Typemock guys went an extra mile and provided support for mocking mscorlib – DateTime.Now and File.ReadAllText(), perhaps more to follow. Awesome.

Moles
Free.

Does not rely on Dependency Injection pattern.

Type safe.

Advanced features: supports recursive mocks, partial mocks.

Überadvanced features: can mock static methods, sealed and private classes.

Not open source and still in a very deep beta (current version is 0.16).

Cannot be used without Pex.

A bit cumbersome syntax.

Mocks need to be regenerated each time you change the system under test.

Moles is designed to suppot a "parametrized unit tests generator" called Pex, which purports to change the face of unit testing forever: instead of creating mocks dynamically, Pex generates them at compile time.

One consequence of static generation is that mocks work extremely fast (however, notice that performance comparison was done for Stubs, not Moles).

As Isolator, Moles uses CLR profiler to intercept method calls – thus being able to mock static methods, etc.

The reason for NMock2, Moq and Rhino Mocks for being unable to mock non-virtual methods lies deep inside the frameworks. They all use Castle Dynamic Proxy to proxy the types they need to mock, and Dynamic Proxy cannot intercept calls to non-virtual, non-abstract methods.

That’s that. Happy mocking!

If you want to see all frameworks in action – there’s Mocking Frameworks Compare open source project that provides a unified set of tests written against Moq, NMock2, Rhino Mocks, Isolator and Pex Stubs.

Categories: Tech
  1. Wednesday, August 17, 2011 at 4:26 am | #1

    Nice post, I din’t know that moles can mock static, non virtual members. I gonna try it…(I thought that it were just another framework)
    I usually use Rhino and works great, but sometimes i miss the ability to mock static methods….

    • Thursday, August 18, 2011 at 9:22 am | #2

      Right, if you are testers that doing a lot of automated testing, you might use moles will give you some advantage.

  2. DShah
    Monday, March 26, 2012 at 8:40 pm | #3

    Hi, we have just started using moles and it takes forever to build test project. I guess because the entity class library has around 4000 classes? Have you had a go at Telerik’s Just Mock? is that any better?

  1. Thursday, August 18, 2011 at 8:47 pm | #1

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.