moq

The most popular and friendly mocking framework for .NET

Latest version: 4.20.72 registry icon
Maintenance score
100
Safety score
100
Popularity score
100
Check your open source dependency risks. Get immediate insight about security, stability and licensing risks.
Security
  Vulnerabilities
Version Suggest Low Medium High Critical
4.20.72 0 0 0 0 0
4.20.70 0 0 0 0 0
4.20.69 0 0 0 0 0
4.18.4 0 0 0 0 0
4.18.3 0 0 0 0 0
4.18.2 0 0 0 0 0
4.18.1 0 0 0 0 0
4.18.0 0 0 0 0 0
4.17.2 0 0 0 0 0
4.17.1 0 0 0 0 0
4.16.1 0 0 0 0 0
4.16.0 0 0 0 0 0
4.15.2 0 0 0 0 0
4.15.1 0 0 0 0 0
4.14.7 0 0 0 0 0
4.14.6 0 0 0 0 0
4.14.5 0 0 0 0 0
4.14.4 0 0 0 0 0
4.14.3 0 0 0 0 0
4.14.2 0 0 0 0 0
4.14.1 0 0 0 0 0
4.14.0 0 0 0 0 0
4.13.1 0 0 0 0 0
4.13.0 0 0 0 0 0
4.12.0 0 0 0 0 0
4.11.0 0 0 0 0 0
4.10.1 0 0 0 0 0
4.10.0 0 0 0 0 0
4.9.0 0 0 0 0 0
4.8.3 0 0 0 0 0
4.8.2 0 0 0 0 0
4.8.1 0 0 0 0 0
4.8.0 0 0 0 0 0
4.7.145 0 0 0 0 0
4.7.142 0 0 0 0 0
4.7.137 0 0 0 0 0
4.7.127 0 0 0 0 0
4.7.99 0 0 0 0 0
4.7.63 0 0 0 0 0
4.7.58 0 0 0 0 0
4.7.49 0 0 0 0 0
4.7.46 0 0 0 0 0
4.7.25 0 0 0 0 0
4.7.12 0 0 0 0 0
4.7.11 0 0 0 0 0
4.7.10 0 0 0 0 0
4.7.9 0 0 0 0 0
4.7.8 0 0 0 0 0
4.7.7 0 0 0 0 0
4.7.1 0 0 0 0 0
4.7.0 0 0 0 0 0
4.5.30 0 0 0 0 0
4.5.29 0 0 0 0 0
4.5.28 0 0 0 0 0
4.5.23 0 0 0 0 0
4.5.22 0 0 0 0 0
4.5.21 0 0 0 0 0
4.5.20 0 0 0 0 0
4.5.19 0 0 0 0 0
4.5.18 0 0 0 0 0
4.5.16 0 0 0 0 0
4.5.13 0 0 0 0 0
4.5.10 0 0 0 0 0
4.5.9 0 0 0 0 0
4.5.8 0 0 0 0 0
4.5.7 0 0 0 0 0
4.5.3 0 0 0 0 0
4.5.0 0 0 0 0 0
4.2.1510.2205 0 0 0 0 0
4.2.1507.118 0 0 0 0 0
4.2.1506.2515 0 0 0 0 0
4.2.1506.2016 0 0 0 0 0
4.2.1502.911 0 0 0 0 0
4.2.1409.1722 0 0 0 0 0
4.2.1408.717 0 0 0 0 0
4.2.1408.619 0 0 0 0 0
4.2.1402.2112 0 0 0 0 0
4.2.1312.1622 0 0 0 0 0
4.2.1312.1621 0 0 0 0 0
4.2.1312.1615 0 0 0 0 0
4.2.1312.1416 0 0 0 0 0
4.2.1312.1323 0 0 0 0 0
4.2.1312.1319 0 0 0 0 0
4.1.1311.615 0 0 0 0 0
4.1.1309.1617 0 0 0 0 0
4.1.1309.919 0 0 0 0 0
4.1.1309.801 0 0 0 0 0
4.1.1309.800 0 0 0 0 0
4.1.1308.2321 0 0 0 0 0
4.1.1308.2316 0 0 0 0 0
4.1.1308.2120 0 0 0 0 0
4.0.10827 0 0 0 0 0
3.1.416.3 0 0 0 0 0

Stability
Latest release:

4.20.72 - This version is safe to use because it has no known security vulnerabilities at this time. Find out if your coding project uses this component and get notified of any reported security vulnerabilities with Meterian-X Open Source Security Platform

Licensing

Maintain your licence declarations and avoid unwanted licences to protect your IP the way you intended.

BSD-3-Clause   -   BSD 3-Clause "New" or "Revised" License

Not a wildcard

Not proprietary

OSI Compliant



moq

Version Downloads Sponsors

The most popular and friendly mocking library for .NET

  var mock = new Mock<ILoveThisLibrary>();

  // WOW! No record/replay weirdness?! :)
  mock.Setup(library => library.DownloadExists("2.0.0.0"))
      .Returns(true);

  // Use the Object property on the mock to get a reference to the object
  // implementing ILoveThisLibrary, and then exercise it by calling
  // methods on it
  ILoveThisLibrary lovable = mock.Object;
  bool download = lovable.DownloadExists("2.0.0.0");

  // Verify that the given method was indeed called with the expected value at most once
  mock.Verify(library => library.DownloadExists("2.0.0.0"), Times.AtMostOnce());

Moq also is the first and only library so far to provide Linq to Mocks, so that the same behavior above can be achieved much more succinctly:

  ILoveThisLibrary lovable = Mock.Of<ILoveThisLibrary>(l =>
    l.DownloadExists("2.0.0.0") == true);

  // Exercise the instance returned by Mock.Of by calling methods on it...
  bool download = lovable.DownloadExists("2.0.0.0");

  // Simply assert the returned state:
  Assert.True(download);
  
  // If you want to go beyond state testing and want to 
  // verify the mock interaction instead...
  Mock.Get(lovable).Verify(library => library.DownloadExists("2.0.0.0"));

You can think of Linq to Mocks as "from the universe of mocks, give me one whose behavior matches this expression".

Check out the Quickstart for more examples!

What?

Moq (pronounced "Mock-you" or just "Mock") is the only mocking library for .NET developed from scratch to take full advantage of .NET Linq expression trees and lambda expressions, which makes it the most productive, type-safe and refactoring-friendly mocking library available. And it supports mocking interfaces as well as classes. Its API is extremely simple and straightforward, and doesn't require any prior knowledge or experience with mocking concepts.

Why?

The library was created mainly for developers who aren't currently using any mocking library (or are displeased with the complexities of some other implementation), and who are typically manually writing their own mocks (with more or less "fanciness"). Most developers in this situation also happen to be quite pragmatic and adhere to state (or classic) TDD. It's the result of feeling that the barrier of entry from other mocking libraries is a bit high, and a simpler, more lightweight and elegant approach is possible. Moq achieves all this by taking full advantage of the elegant and compact C# and VB language features collectively known as LINQ (they are not just for queries, as the acronym implies).

Moq is designed to be a very practical, unobtrusive and straight-forward way to quickly setup dependencies for your tests. Its API design helps even novice users to fall in the "pit of success" and avoid most common misuses/abuses of mocking.

When it was conceived, it was the only mocking library that went against the generalized and somewhat unintuitive (especially for novices) Record/Replay approach from all other libraries (and that might have been a good thing ;)).

Not using Record/Replay also means that it's straightforward to move common expectations to a fixture setup method and even override those expectations when needed in a specific unit test.

You can read more about the "why" and see some nice screenshots at kzu's blog.

Where?

See our Quickstart examples to get a feeling of the extremely simple API and install from NuGet.

Read about the announcement at kzu's blog. Get some background on the state of mock libraries from Scott Hanselman.

Who?

Moq was originally developed by Clarius, Manas and InSTEDD.

Moq uses Castle DynamicProxy internally as the interception mechanism to enable mocking.

Features at a glance

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/Replay idioms to learn. Just construct your mock, set it up, use it and optionally verify calls to it (you may not verify mocks when they act as stubs only, or when you are doing more classic state-based testing by checking returned values from the object under test)
  • 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
  • Intuitive support for out/ref arguments

We appreciate deeply any feedback that you may have! Feel free to participate in the chat, or report an issue in the issue tracker.

Sponsors

Clarius Org Kirill Osenkov MFB Technologies, Inc. Torutek DRIVE.NET, Inc. Keith Pickford Thomas Bolon Kori Francis Toni Wenzel Uno Platform Dan Siegel Reuben Swartz Jacob Foshee Eric Johnson Ix Technologies B.V. David JENNI Jonathan Charley Wu Jakob Tikjøb Andersen Seann Alexander Tino Hager Mark Seemann Ken Bonny Simon Cropp agileworks-eu sorahex Zheyu Shen Vezel ChilliCream 4OTC Vincent Limo

Sponsor this project  

Learn more about GitHub Sponsors