Moq 4.5.8

Moq is the most popular and friendly mocking framework for .NET

There is a newer version of this package available.
See the version list below for details.
Install-Package Moq -Version 4.5.8
dotnet add package Moq --version 4.5.8
<PackageReference Include="Moq" Version="4.5.8" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Moq --version 4.5.8
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

Release Notes

Version 4.5
* Updated to .NET 4.5
* Dropped support for .NET < 4.5 and Silverlight
* Remove ILMerge. Depend on Castle NuGet package instead.

Version 4.3
* Added support for Roslyn
* Automatically add implemented interfaces to mock

Version 4.2
* Improved support for async APIs by making default value a completed task
* Added support for async Returns and Throws
* Improved mock invocation sequence testing
* Improved support for multi-threaded tests
* Added support for named mocks

Version 4.1
* Added covariant IMock<out T> interface to Mock<T>
* Added It.IsNotNull<T>
* Fix: 'NullReferenceException when subscribing to an event'
* Added overloads to Verify to accept Times as a Method Group
* Feature request: It.IsIn(..), It.IsNotIn(...)
* Corrected Verify method behavior for generic methods calls
* Differentiate verification error from mock crash
* Fix: Adding (and removing) handlers for events declared on interfaces works
when CallBase = true.
* Update to latest Castle
* Fix: Mock.Of (Functional Syntax) doesn't work on properties with non-public setters
* Fix: Allow to use CallBase instead of Returns
* Fix: Solved Multi-threading issue - IndexOutOfRangeException
* Capability of mocking delegates (event handlers)

Version 4.0
* Linq to Mocks: Mock.Of<T>(x => x.Id == 23 && x.Title == "Rocks!")
* Fixed issues:
 *  87 BadImageFormatException when using a mock with a Visual Studio generated Accessor object
 *  166 Unable to use a delegate to mock a function that takes 5 or more parameters.
 *  168 Call count failure message never says which is the actual invocation count
 *  175 theMock.Object failing on VS2010 Beta 1
 *  177 Generic constraint on interface method causes BadImageFormatException when getting Object.
 *  183 Display what invocations were recieved when the expected one hasn't been met
 *  186 Methods that are not virtual gives non-sense-exception message
 *  188 More Callback Overloads
 *  199 Simplify SetupAllProperties implementation to simply iterate and call SetupProperty
 *  200 Fluent mock does not honor parent mock CallBase setting.
 *  202 Mock.Protected().Expect() deprecated with no work-around
 *  204 Allow default return values to be specified (per-mock)
 *  205 Error calling SetupAllProperties for Mock<IDataErrorInfo>
 *  206 Linq-to-Mocks Never Returns on Implicit Boolean Property
 *  207 NullReferenceException thrown when using Mocks.CreateQuery with implicit boolean expression
 *  208 Can't setup a mock for method that accept lambda expression as argument.
 *  211 SetupAllProperties should return the Mock<T> instead of void.
 *  223 When a method is defined to make the setup an asserts mock fails
 *  226 Can't raise events on mocked Interop interfaces
 *  229 CallBase is not working for virtual events
 *  238 Moq fails to mock events defined in F#
 *  239 Use Func instead of Predicate
 *  250 4.0 Beta 2 regression - cannot mock MethodInfo when targetting .NET 4
 *  251 When a generic interface also implements a non-generic version, Verify does not work in some cases
 *  254 Unable to create mock of EnvDTE.DTE
 *  261 Can not use protected setter in public property
 *  267 Generic argument as dependency for method Setup overrides all previous method setups for a given method
 *  273 Attempting to create a mock thrown a Type Load exception. The message refers to an inaccessible interface.
 *  276 .Net 3.5 no more supported

Version 3.0

* Silverlight support! Finally integrated Jason's Silverlight contribution! Issue #73
* Brand-new simplified event raising syntax (#130): mock.Raise(foo => foo.MyEvent += null, new MyArgs(...));
* Support for custom event signatures (not compatible with EventHandler): mock.Raise(foo => foo.MyEvent += null, arg1, arg2, arg3);
* Substantially improved property setter behavior: mock.VerifySet(foo => foo.Value = "foo");  //(also available for SetupSet
* Renamed Expect* with Setup*
* Vastly simplified custom argument matchers: public int IsOdd() < return Match<int>.Create(v => i % 2 == 0); >
* Added support for verifying how many times a member was invoked: mock.Verify(foo => foo.Do(), Times.Never());
* Added simple sample app named StoreSample
* Moved Stub functionality to the core API (SetupProperty and SetupAllProperties)
* Fixed sample ASP.NET MVC app to work with latest version
* Allow custom matchers to be created with a substantially simpler API
* Fixed issue #145 which prevented discrimination of setups by generic method argument types
* Fixed issue #141 which prevented ref arguments matching value types (i.e. a Guid)
* Implemented improvement #131: Add support for It.IsAny and custom argument matchers for SetupSet/VerifySet
* Implemented improvement #124 to render better error messages
* Applied patch from David Kirkland for improvement #125 to improve matching of enumerable parameters
* Implemented improvement #122 to provide custom errors for Verify
* Implemented improvement #121 to provide null as default value for Nullable<T>
* Fixed issue #112 which fixes passing a null argument to a mock constructor
* Implemented improvement #111 to better support params arguments
* Fixed bug #105 about improperly overwriting setups for property getter and setter
* Applied patch from Ihar.Bury for issue #99 related to protected expectations
* Fixed issue #97 on not being able to use SetupSet/VerifySet if property did not have a getter
* Better integration with Pex (https://research.microsoft.com/en-us/projects/Pex/)
* Various other minor fixes (#134, #135, #137, #138, #140, etc.)
   

Version 2.6

* Implemented Issue #55: We now provide a mock.DefaultValue = [DefaultValue.Empty | DefaultValue.Mock] which will provide the current behavior (default) or mocks for mockeable return types for loose mock invocations without expectations.
* Added support for stubbing properties from moq-contrib: now you can do mock.Stub(m => m.Value) and add stub behavior to the property. mock.StubAll() is also provided. This integrates with the DefaultValue behavior too, so you can stub entire hierarchies :).
* Added support for mocking methods with out and ref parameters (Issue #50)
* Applied patch contributed by slava for Issue #72: add support to limit numbor of calls on mocked method (we now have mock.Expect(...).AtMost(5))
* Implemented Issue #94: Easier setter verification: Now we support ExpectSet(m = m.Value, "foo") and VerifySet(m = m.Value, 5) (Thanks ASP.NET MVC Team!)
* Implemented issue #96: Automatically chain mocks when setting expectations. It's now possible to specify expectations for an entire hierarchy of objects just starting from the root mock. THIS IS REALLY COOL!!!
* Fixed Issue #89: Expects() does not always return last expectation
* Implemented Issue 91: Expect a method/property to never be called (added Never() method to an expectation. Can be used on methods, property getters and setters)
* Fixed Issue 86: IsAny<T> should check if the value is actually of type T
* Fixed Issue 88: Cannot mock protected internal virtual methods using Moq.Protected
* Fixed Issue 90: Removing event handlers from mocked objects
* Updated demo and added one more test for the dynamic addition of interfaces

Version 2.5

* Added support for mocking protected members
* Added new way of extending argument matchers which is now very straightforward
* Added support for mocking events
* Added support for firing events from expectations
* Removed usage of MBROs which caused inconsistencies in mocking features
* Added ExpectGet and ExpectSet to better support properties, and provide better intellisense.
* Added verification with expressions, which better supports Arrange-Act-Assert testing model (can do Verify(m => m.Do(...)))
* Added Throws<TException>
* Added mock.CallBase property to specify whether the virtual members base implementation should be called
* Added support for implementing and setting expectations and verifying additional interfaces in the mock, via the new mock.As<TInterface>() method (thanks Fernando Simonazzi!)
* Improved argument type matching for Is/IsAny  (thanks Jeremy.Skinner!)


Version 2.0

* Refactored fluent API on mocks. This may cause some existing tests to fail, but the fix is trivial (just reorder the calls to Callback, Returns and Verifiable)
* Added support for retrieving a Mock<T> from a T instance created by a mock.
* Added support for retrieving the invocation arguments from a Callback or Returns.
* Implemented AtMostOnce() constraint
* Added support for creating MBROs with protected constructors
* Loose mocks now return default empty arrays and IEnumerables instead of nulls


Version 1.5.1

* Refactored MockFactory to make it simpler and more explicit to use with regards to verification. Thanks Garry Shutler for the feedback!

Version 1.5

* Added MockFactory to allow easy construction of multiple mocks with the same behavior and verification

Version 1.4

* Added support for passing constructor arguments for mocked classes.
* Improved code documentation

Version 1.3

* Added support for overriding expectations set previously on a Mock. Now adding a second expectation for the same method/property call will override the existing one. This facilitates setting up default expectations in a fixture setup and overriding when necessary in a specific test.
* Added support for mock verification. Both Verify and VerifyAll are provided for more flexibility (the former only verifies methods marked Verifiable)

Version 1.2

* Added support for MockBehavior mock constructor argument to affect the way the mocks expect or throw on calls.

Version 1.1

* Merged branch for dynamic types. Now Moq is based on Castle DynamicProxy2 to support a wider range of mock targets.
* Added ILMerge so that Castle libraries are merged into Moq assembly (no need for external references and avoid conflicts)

Version 1.0

* Initial release, initial documentation process in place, etc.

Showing the top 10 GitHub repositories that depend on Moq:

Repository Stars
dotnet/corefx
CoreFX is the foundational class libraries for .NET Core. It includes types for collections, file systems, console, JSON, XML, async and many others.
Wox-launcher/Wox
Launcher for Windows, an alternative to Alfred and Launchy.
dotnet/roslyn
The Roslyn .NET compiler provides C# and Visual Basic languages with rich code analysis APIs.
dotnet-architecture/eShopOnContainers
Easy to get started sample reference microservice and container based application. Cross-platform on Linux and Windows Docker Containers, powered by .NET Core 2.2, Docker engine and optionally Azure, Kubernetes or Service Fabric. Supports Visual Studio, VS for Mac and CLI based environments with Docker CLI, dotnet CLI, VS Code or any other code editor.
SignalR/SignalR
Incredibly simple real-time web for .NET
JamesNK/Newtonsoft.Json
Json.NET is a popular high-performance JSON framework for .NET
AvaloniaUI/Avalonia
A multi-platform .NET UI framework
restsharp/RestSharp
Simple REST and HTTP API Client for .NET
App-vNext/Polly
Polly is a .NET resilience and transient-fault-handling library that allows developers to express policies such as Retry, Circuit Breaker, Timeout, Bulkhead Isolation, and Fallback in a fluent and thread-safe manner. From version 6.0.1, Polly targets .NET Standard 1.1 and 2.0+.
cefsharp/CefSharp
.NET (WPF and Windows Forms) bindings for the Chromium Embedded Framework

Version History

Version Downloads Last updated
4.13.0 125,708 8/31/2019
4.12.0 1,827,790 6/20/2019
4.11.0 915,499 5/27/2019
4.11.0-rc2 3,246 5/27/2019
4.11.0-rc1 56,691 4/19/2019
4.10.1 8,445,250 12/3/2018
4.10.0 5,190,081 9/8/2018
4.9.0 3,342,534 7/13/2018
4.8.3 2,131,964 6/9/2018
4.8.2 5,429,927 2/23/2018
4.8.1 2,424,324 1/8/2018
4.8.0 628,300 12/24/2017
4.8.0-rc1 34,499 12/8/2017
4.7.145 3,401,948 11/6/2017
4.7.142 1,585,936 10/10/2017
4.7.137 392,365 10/3/2017
4.7.127 415,505 9/25/2017
4.7.99 3,305,863 7/17/2017
4.7.63 969,395 6/21/2017
4.7.58 55,951 6/20/2017
4.7.49 292,106 6/18/2017
4.7.46 24,116 6/18/2017
4.7.25 690,725 6/2/2017
4.7.12 147,454 5/30/2017
4.7.11 3,570 5/30/2017
4.7.10 714,539 5/6/2017
4.7.9 264,219 4/29/2017
4.7.8 1,033,230 3/26/2017
4.7.7 23,250 3/25/2017
4.7.1 1,050,352 2/28/2017
4.7.0 846,357 2/22/2017
4.6.62-alpha 4,539 2/21/2017
4.6.39-alpha 9,083 2/18/2017
4.6.38-alpha 616,453 8/20/2016
4.6.36-alpha 184,165 7/20/2016
4.6.25-alpha 85,352 6/6/2016
4.5.30 1,473,364 1/9/2017
4.5.29 752,488 12/10/2016
4.5.28 852,115 11/10/2016
4.5.23 699,313 10/11/2016
4.5.22 436,172 9/20/2016
4.5.21 954,913 8/12/2016
4.5.20 7,988 8/12/2016
4.5.19 54,156 8/10/2016
4.5.18 4,620 8/10/2016
4.5.16 431,429 7/18/2016
4.5.13 120,886 7/11/2016
4.5.10 364,675 6/21/2016
4.5.9 361,254 6/9/2016
4.5.9-alpha 8,211 5/22/2016
4.5.8 496,494 5/26/2016
4.5.7 6,000 5/26/2016
4.5.7-alpha 3,080 5/22/2016
4.5.6-alpha 3,187 5/22/2016
4.5.3 292,494 5/25/2016
4.5.0 125,709 5/24/2016
4.2.1510.2205 4,267,765 10/22/2015
4.2.1507.118 1,579,315 7/1/2015
4.2.1506.2515 81,322 6/25/2015
4.2.1506.2016 50,152 6/20/2015
4.2.1502.911 1,782,577 2/9/2015
4.2.1409.1722 1,238,595 9/17/2014
4.2.1408.717 645,805 8/7/2014
4.2.1408.619 22,935 8/6/2014
4.2.1402.2112 1,503,185 2/21/2014
4.2.1312.1622 549,987 12/17/2013
4.2.1312.1621 4,360 12/16/2013
4.2.1312.1615 4,926 12/16/2013
4.2.1312.1416 16,622 12/14/2013
4.2.1312.1323 4,162 12/14/2013
4.2.1312.1319 14,324 12/13/2013
4.1.1311.615 243,965 11/18/2013
4.1.1309.1617 367,718 9/16/2013
4.1.1309.919 244,464 9/9/2013
4.1.1309.801 9,348 9/8/2013
4.1.1309.800 4,061 9/8/2013
4.1.1308.2321 438,141 8/23/2013
4.1.1308.2316 5,325 8/23/2013
4.1.1308.2120 234,296 8/21/2013
4.0.10827 2,462,728 2/9/2011
3.1.416.3 91,165 2/9/2011
Show less