Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the real-world pros and cons of each of the major mocking frameworks? [closed]

see also "What should I consider when choosing a mocking framework for .Net"

I'm trying to decide on a mocking framework to use on a .NET project I've recently embarked on. I'd like to speed my research on the different frameworks. I've recently read this blog post http://codevanced.net/post/Mocking-frameworks-comparison.aspx and wondered if any of the StackOverflow audience has anything to add in the way of real-world advantages and caveats to the frameworks.

Could people could list the pros/cons of the mocking frameworks they either currently use or have investigated for their own use on .NET projects. I think this would be not only a help to me to decide for my current project, but it will help others make more informed decisions when picking the correct framework for their situation. I'm not an expert on any of the frameworks but I would like to get arguments for and against the major frameworks I've come across:

  • RhinoMocks
  • Moq
  • TypeMock Isolator
  • NMock
  • Moles

And other usable alternatives that I've missed. I'd also like insights from users that have switched or stopped using products because of issues.

like image 556
BenAlabaster Avatar asked Nov 11 '09 22:11

BenAlabaster


People also ask

When should you not use a mock?

Only use a mock (or test double) “when testing things that cross the dependency inversion boundaries of the system” (per Bob Martin). If I truly need a test double, I go to the highest level in the class hierarchy diagram above that will get the job done. In other words, don't use a mock if a spy will do.

What does mocking a class allow you to do?

The purpose of mocking is to isolate and focus on the code being tested and not on the behavior or state of external dependencies. In mocking, the dependencies are replaced by closely controlled replacements objects that simulate the behavior of the real ones.

Why is mocking a code smell?

A code smell does not mean that something is definitely wrong, or that something must be fixed right away. It is a rule of thumb that should alert you to a possible opportunity to improve something. This text and its title in no way imply that all mocking is bad, or that you should never mock anything.


2 Answers

I don't know Moles at all, but I'll cover the ones I know a bit about (I really need a table for this, though).

Moq

Pros

  • Type-safe
  • Consistent interface
  • Encourages good design

Cons

  • Not as full-featured as some of its competitors
    • It can't mock delegates
    • It can't do ordered expectations
    • probably other things I can't think of right now...
  • Can only mock interfaces and virtual/abstract members

Rhino Mocks

Pros

  • Type-safe
  • Full feature set
  • Encourages good design

Cons

  • Confusing API. There are too many different ways to do the same things, and if you combine them in the wrong way it just doesn't work.
  • Can only mock interfaces and virtual/abstract members

TypeMock Isolator

Pros

  • Type-safe (AFAIR)
  • Can mock anything

Cons

  • Very invasive
  • Potential Vendor Lock-In
  • Does not encourage good design

NMock

Pros

  • Encourages good design
  • Works on any version of .NET (even 1.1)

Cons

  • Not type-safe
  • Can only mock interfaces and virtual/abstract members

Please note that particularly the advantages and disadvantages regarding TypeMock are highly controversial. I published my own take on the matter on my blog.

I started out with NMock when that was the only option back in 2003, then migrated to Rhino Mocks because of its type safety, and now use Moq because of the simpler API.

like image 175
Mark Seemann Avatar answered Sep 18 '22 20:09

Mark Seemann


So far I have used RhinoMocks and Moq. Moq is currently my favourite due to its simplicity which is currently all I need. RhinoMocks is pretty powerful but I have never been in the position to fully tap into it.

like image 42
flq Avatar answered Sep 18 '22 20:09

flq