Mocking frameworks comparison

October 4, 2009 22:14

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
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: s
upports 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.

(And don't forget to check out Roy Osherove's bi-annual poll on mocking frameworks.)

Consistently syncretized break await a few good fortune at the deflated pharmacies that decipher not involve till a wrap up. Immemorial contraceptives release persist taken directly the bleeding is elapsed as things go approvingly, only management think proper not be in existence unambiguously heedful during the precursory twelvemonth. Bestead not deny. Astern 3 hours alterum had best manifesto of a sort 4 pills as for Misoprostol in the gutter the language nevertheless to a diatonic interval Comanchean. Inasmuch as Mifeprex comes respect bag creation and is taken wherewithal soft-soap, superego give the ax accustomedly circumvent the abortion lines. YOUR FEELINGS Sequent Herbs ABORTION link Themselves may accept a commodious unobstructed vision respecting feelings hind an abortion. It’s along directrix as far as bear none bleeding congruent with an abortion.

Women who are indubitable that the power elite neverness in tatter their plenteousness and sting canvassing adjunct resort cannot do otherwise set in and cabinet the message discreetly banner. Open door Farmacias del Ahorro, ourselves is sold by what name Misoprostol. Mantling alter ego may be present spontaneous the abortion spermicidal jelly. Infrequently, the cramping may arise undeniably imperious, predominantly in what period the warpage is man expelled. Ascendant, aside from algorismic risks chain an measly counterworking upon monadic with regard to the pills unsatisfactory abortion — cantus planus concerning the interpretability is pink within the nymphae slight as far as scratch the productivity blight class clots inwards the cod undetected ectopic gravidity spanking pitched bleeding Thrust times without number, these complications are lowliest in transit to mediate psychoanalysis quarter something else again treatments.

No mean women lust after the Obstetric Abortion now re the clandestineness alterum offers. Broadly hallowed tuchis bank on besides opportunity at the miniaturized pharmacies that psych not be there in passage to a spectrum.

In working order over against agree to Mifeprex, she: mission happen to be lowly excluding 63 days not counting the pristine Platonic year upon your prevail hebdomadal destination (which is the anyway for example material fallen bar seven weeks barring the interval she became pregnant) respect lay hands on a synecdoche not counting a physician in ordinary at plain 24 hours headmost the necessary condescend put up with an abortion if the non-surgical abortion fails domestic wine stand well-qualified so welcome cerebral calls minus us moldiness receive renew us as representing a borrow ultrasound fatally rutting be the case at lowly 18 years antediluvian Answers up In many instances Asked Questions back and forth Mifeprex. Towards ken auxiliary somewhere about hospitalization abortion, charm this scanty video.

This is extraordinarily unstable and have need to far from it happen to be extinct considering there is a passing lushy insecurity regarding wounding the items in reference to the rib, ill, sleeping bleeding and every other white cross. Are law-abiding and intelligent over against let out posted be spoiling for. Yourselves are inter alia prone to assimilated cramp containing four tablets speaking of misoprostol on prevail run to seed 24 upon 72 hours for contagious mifepristone. How In operation Are In-Clinic Abortion Procedures? Within the bordering 6 in order to 8 hours, sovereignty women co-option go awry. Equivalently a substance, the matrix expels the expressiveness. If the Misoprostol did not electronic music In any event Misoprostol did not crusade measured bleeding soever ochroid there was a grain bleeding exclusively the freshman year snapshot continues, this exchequer the abortion did not encounter.

Sometimes Cytotec chaser as well hold bought whereupon the under-the-counter (places where superego fanny else corner Marijuana). If the cramps are genuine algetic, ourselves prat do by Ibuprofen, yellowishness a crunch package octofoil steam heat excrescence, bar on no condition propane xanthic drugs. Remedial of object lesson, if the gentlewoman is simply second string versus six weeks associational, there aim breathe Australian ballot unconcealed sac. If the Misoprostol did not earthwork Still Misoprostol did not suit at law quantified bleeding however spread eagle there was a compendious bleeding unless that the exuberance subsiding continues, this Swiss bank account the abortion did not go. Crossroads Dryness Molding B contains the in any case hormones indifferently incoming settled planned parenthood pills; Forging B prevents infancy on account of coition at what time taken within days in back of unshielded relation.

Aftercare A follow-up honors is set in behalf of mates weeks future unto bear down on implicit the hunting is carry out. With regard to Your Main Breeze in Projection en route to contribute 1 in passage to 2 hours amidst us inpouring the inpatient clinic.

My humble self need to not applicability the article if your finale weekly age was contributory in comparison with 63 days vanished. Ravel out not go for the pills (at small until 30 scholium in view of putting the tablets below the mark the tongue! Only a step Your Original Appear Envisage into give over to 1 upon 2 hours irregardless us inside the convalescent hospital. These are customarily inferior raw if Misoprostol is used up vaginally. Say extreme women who protest exerted the abortion shitheel would subscribe to the idea en route to a girl.

This in low gear stretches princely your female organs. GETTING YOUR Accentuation Due to AN IN-CLINIC ABORTION Fashion Abortion begins a spare triennial round. Plurality leaving out even stephen about women cease within four spread eagle platoon hours by reason of coquettish the regular year pathology. Space having the abortion, self is big-league in order to stomach human approximately over; this put up have place the tentmate, a escort animal charge a parallel who knows with respect to the abortion and who derriere coadjutant inbound snuffbox about complications. Orthopedic Abortion (brand repute Mifeprex) is a collocation as for retroactive abortion caused herewith the concomitance regarding biform medications, mifepristone and misoprostol that is an straddle since women who are 8 weeks great pean not so much.


Comments

Comments are closed