Moq .NET Mocking Library

| by Al Tenhundfeld Follow 0 Followers on Aug 12, 2008. Estimated reading time: 1 minute |

A note to our readers: As per your request we have developed a set of features that allow you to reduce the noise, while not losing sight of anything that is important. Get email and web notifications by choosing the topics you are interested in.

Moq is a mocking library for .NET designed and developed to utilize .NET 3.5 features, e.g., Linq expression trees and lambda expressions. Moq's goal is to be simple and straightforward, allowing a natural integration into existing unit tests, instead of forcing developers to rewrite tests or learn extensive Record/Replay mocking frameworks. Moq targets those developers who are writing tests but are not using mocking frameworks. The design principle behind Moq is to be refactoring friendly with a very low barrier of entry.

Moq offers the following features:
  • Strong-typed: no strings for expectations, no object-typed return values or constraints
  • Unsurpassed VS intellisense integration: everything supports full VS intellisense, from setting expectations, to specifying method call arguments, return values, etc.
  • No Record/Reply idioms to learn. Just construct your mock, set your expectations, use it and optionally verify them
  • VERY low learning curve as a consequence of the previous three points. For the most part, you don't even need to ever read the documentation.
  • Granular control over mock behavior with a simple MockBehavior enumeration (no need to learn what's the theoretical difference between a mock, a stub, a fake, a dynamic mock, etc.)
  • Mock both interfaces and classes
  • Override expectations: can set default expectations in a fixture setup, and override as needed on tests
  • Pass constructor arguments for mocked classes
  • Intercept and raise events on mocks
  • Moq is open source and eager for more contributors. Along with the source, the binaries and a getting started guide are hosted on Google Code.

    Daniel Cazzulino, Moq's active maintainer, has a series of posts that provide his reasoning for creating Moq and several good examples of how to use Moq effectively.

    Rate this Article

    Adoption Stage

    Hello stranger!

    You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

    Get the most out of the InfoQ experience.

    Tell us what you think

    Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

    Email me replies to any of my messages in this thread
    Community comments

    Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

    Email me replies to any of my messages in this thread

    Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

    Email me replies to any of my messages in this thread


    Login to InfoQ to interact with what matters most to you.

    Recover your password...


    Follow your favorite topics and editors

    Quick overview of most important highlights in the industry and on the site.


    More signal, less noise

    Build your own feed by choosing topics you want to read about and editors you want to hear from.


    Stay up-to-date

    Set up your notifications and don't miss out on content that matters to you