Mocking frameworks comparison, part 1: Introduction

This series is inspired by recent post by Roy Osherove (and its comments) where he asks readers what isolation framework should be featured in second edition of the Art of Unit Testing (by featured I mean used in code samples). Few commenters agreed that some of the frameworks differ in syntax only and I thought it would be educational to check how far those differences go.

I'll compare most popular players in open source mocking framework world, which would be: RhinoMocks, Moq, FakeItEasy and (in the future) NSubstitute. Before we begin it's important to establish basic definitions as each library uses different terminology, such as:

  • RhinoMocks: mock (partial, static, dynamic), stub with expect, stub
  • Moq: mock with setup
  • FakeItEasy: fake with call to
  • NSubstitute: substitute

I'll simplify this to two verbs (although I admit I'd rather have one - something coming either from fake or substitute) describing how mock objects are used in test (similar to how Roy Osherove defined mock/stub difference in his book):

  • mocking - a process of setting up expectations and verifying whether calls to certain object were made by tested code (can make test fail when expectations are not met)
  • stubbing - a process of setting up dependency object to serve tested code in some way (like providing value or raising event; cannot make test fail)

Mock/stub will be used respectively as subjects of those processes.

This post will also serve as root/navigation post to future parts. I'll try to keep one point from the list below per post ratio, however in some cases there might be exceptions to this rule. Without further due:

  1. Instantiation

  2. Simple mocking
    2.1 Method call
    2.2 Property getter
    2.3 Property setter
    2.4 Indexer

  3. Arguments
    3.1 Ignoring
    3.2 Simple constraints
    3.3 Custom matchers

  4. Throwing exceptions

  5. Events

  6. Advanced scenarios
    6.1 Using base class implementation
    6.2 Returning ref/out parameters
    6.3 Returning from sequence of values
    6.4 Ordered expectations
    6.5 Protected members
    6.6 Custom behavior upon call


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Solve the challenge for fame and fortune!