Moq 4.21.0

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!

Sponsors

Clarius Org MFB Technologies, Inc. SandRock DRIVE.NET, Inc. Keith Pickford Thomas Bolon Reuben Swartz Jacob Foshee Eric Johnson Jonathan Ken Bonny Simon Cropp agileworks-eu Zheyu Shen Vezel ChilliCream 4OTC domischell Adrian Alonso torutek Ryan McCaffery Seika Logiciel Andrew Grant eska-gmbh Geodata AS

Sponsor this project

Learn more about GitHub Sponsors

Showing the top 20 packages that depend on Moq.

Packages Downloads
Moq.AutoMock
An auto-mocking container that generates mocks using Moq
1
Moq.EntityFrameworkCore
Library that provides methods that will help you with mocking Entity Framework Core. Easily mock DbSet and DbContext for unit testing with Moq.
2

.NET Framework 4.6.2

.NET Standard 2.0

.NET Standard 2.1

.NET 6.0

Version Downloads Last updated
4.21.0 2 2026-10-03
4.20.72 376 2025-04-04
4.20.70 1 2025-12-19
4.20.69 0 2023-08-11
4.18.4 0 2022-12-30
4.18.3 0 2022-12-05
4.18.2 0 2022-08-02
4.18.1 0 2022-05-16
4.18.0 0 2022-05-11
4.17.2 1 2026-01-05
4.17.1 0 2022-02-26
4.16.1 0 2021-02-23
4.16.0 0 2021-01-16
4.15.2 0 2020-11-26
4.15.1 0 2020-11-10
4.14.7 0 2020-10-14
4.14.6 0 2020-09-30
4.14.5 0 2020-07-01
4.14.4 0 2020-06-24
4.14.3 0 2020-06-18
4.14.2 0 2020-06-16
4.14.1 0 2020-04-28
4.14.0 0 2020-04-24
4.13.1 0 2019-10-19
4.13.0 0 2019-08-31
4.12.0 0 2019-06-20
4.11.0 0 2019-05-27
4.11.0-rc2 0 2019-05-27
4.11.0-rc1 0 2019-04-19
4.10.1 4 2025-04-09
4.10.0 0 2018-09-08
4.9.0 0 2018-07-13
4.8.3 0 2018-06-09
4.8.2 0 2018-02-23
4.8.1 0 2018-01-08
4.8.0 0 2017-12-24
4.8.0-rc1 0 2017-12-08
4.7.145 0 2017-11-06
4.7.142 0 2017-10-10
4.7.137 1 2025-04-10
4.7.127 0 2017-09-25
4.7.99 4 2025-04-07
4.7.63 0 2017-06-21
4.7.58 0 2017-06-20
4.7.49 0 2017-06-18
4.7.46 0 2017-06-18
4.7.25 0 2017-06-02
4.7.12 0 2017-05-30
4.7.11 0 2017-05-30
4.7.10 0 2017-05-06
4.7.9 0 2017-04-29
4.7.8 0 2017-03-26
4.7.7 0 2017-03-25
4.7.1 0 2017-02-28
4.7.0 0 2017-02-22
4.6.62-alpha 0 2017-02-21
4.6.39-alpha 0 2017-02-18
4.6.38-alpha 0 2016-08-20
4.6.36-alpha 0 2016-07-20
4.6.25-alpha 0 2016-06-06
4.5.30 0 2017-01-09
4.5.29 0 2016-12-10
4.5.28 0 2016-11-10
4.5.23 0 2016-10-11
4.5.22 0 2016-09-20
4.5.21 0 2016-08-12
4.5.20 0 2016-08-12
4.5.19 0 2016-08-10
4.5.18 0 2016-08-10
4.5.16 0 2016-07-18
4.5.13 0 2016-07-11
4.5.10 0 2016-06-21
4.5.9 0 2016-06-09
4.5.9-alpha 0 2016-05-22
4.5.8 0 2016-05-26
4.5.7 0 2016-05-26
4.5.7-alpha 0 2016-05-22
4.5.6-alpha 0 2016-05-22
4.5.3 0 2016-05-25
4.5.0 0 2016-05-24
4.2.1510.2205 0 2015-10-22
4.2.1507.118 0 2015-07-01
4.2.1506.2515 0 2015-06-25
4.2.1506.2016 0 2015-06-20
4.2.1502.911 0 2015-02-09
4.2.1409.1722 0 2014-09-17
4.2.1408.717 0 2014-08-07
4.2.1408.619 0 2014-08-06
4.2.1402.2112 0 2014-02-21
4.2.1312.1622 0 2013-12-17
4.2.1312.1621 0 2013-12-16
4.2.1312.1615 0 2013-12-16
4.2.1312.1416 0 2013-12-14
4.2.1312.1323 0 2013-12-14
4.2.1312.1319 0 2013-12-13
4.1.1311.615 0 2013-11-18
4.1.1309.1617 0 2013-09-16
4.1.1309.919 0 2013-09-09
4.1.1309.801 0 2013-09-08
4.1.1309.800 0 2013-09-08
4.1.1308.2321 0 2013-08-23
4.1.1308.2316 0 2013-08-23
4.1.1308.2120 0 2013-08-21
4.0.10827 0 2011-02-09
3.1.416.3 0 2011-02-09