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.EntityFrameworkCore
Library that provides methods that will help you with mocking Entity Framework Core.
13
Moq.EntityFrameworkCore
Library that provides methods that will help you with mocking Entity Framework Core.
12
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.
12
Moq.EntityFrameworkCore
Library that provides methods that will help you with mocking Entity Framework Core.
11
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.
11
Moq.EntityFrameworkCore
Package Description
11
Moq.EntityFrameworkCore
Library that provides methods that will help you with mocking Entity Framework Core.
10

.NET Framework 4.6.2

.NET Standard 2.0

.NET Standard 2.1

.NET 6.0

Version Downloads Last updated
4.21.0 1 09/24/2026
4.20.72 1,299 12/31/2025
4.20.71 38 12/14/2025
4.20.70 1,098 12/10/2025
4.20.69 35 12/30/2025
4.20.2 39 12/14/2025
4.20.1 40 12/28/2025
4.20.0 39 12/14/2025
4.20.0-rc 42 01/13/2026
4.20.0-beta 36 12/14/2025
4.20.0-alpha 24 12/11/2025
4.18.4 43 12/12/2025
4.18.3 31 12/14/2025
4.18.2 37 12/12/2025
4.18.1 34 12/13/2025
4.18.0 32 12/31/2025
4.17.2 36 12/20/2025
4.17.1 44 12/09/2025
4.16.1 34 12/14/2025
4.16.0 39 12/18/2025
4.15.2 39 12/13/2025
4.15.1 37 12/15/2025
4.14.7 45 01/01/2026
4.14.6 44 12/12/2025
4.14.5 37 12/11/2025
4.14.4 33 12/18/2025
4.14.3 38 12/18/2025
4.14.2 32 12/31/2025
4.14.1 37 12/15/2025
4.14.0 44 12/20/2025
4.13.1 36 12/31/2025
4.13.0 36 12/31/2025
4.12.0 48 12/14/2025
4.11.0 41 12/13/2025
4.11.0-rc2 43 12/10/2025
4.11.0-rc1 45 12/10/2025
4.10.1 39 12/13/2025
4.10.0 35 12/20/2025
4.9.0 41 12/14/2025
4.8.3 35 12/20/2025
4.8.2 41 12/20/2025
4.8.1 43 12/09/2025
4.8.0 40 12/10/2025
4.8.0-rc1 38 12/20/2025
4.7.145 36 12/20/2025
4.7.142 36 12/15/2025
4.7.137 36 12/10/2025
4.7.127 35 12/31/2025
4.7.99 38 12/14/2025
4.7.63 45 12/10/2025
4.7.58 37 12/31/2025
4.7.49 41 12/31/2025
4.7.46 41 12/20/2025
4.7.25 41 12/17/2025
4.7.12 47 12/13/2025
4.7.11 41 12/13/2025
4.7.10 42 12/12/2025
4.7.9 40 12/15/2025
4.7.8 32 12/20/2025
4.7.7 37 12/20/2025
4.7.1 38 12/20/2025
4.7.0 40 12/20/2025
4.6.62-alpha 37 12/20/2025
4.6.39-alpha 42 12/18/2025
4.6.38-alpha 32 12/20/2025
4.6.36-alpha 38 12/20/2025
4.6.25-alpha 36 12/20/2025
4.5.30 33 12/31/2025
4.5.29 38 12/20/2025
4.5.28 31 01/01/2026
4.5.23 37 12/20/2025
4.5.22 35 12/10/2025
4.5.21 26 12/31/2025
4.5.20 36 12/18/2025
4.5.19 34 12/20/2025
4.5.18 31 12/29/2025
4.5.16 30 12/31/2025
4.5.13 40 12/31/2025
4.5.10 35 12/20/2025
4.5.9 45 12/13/2025
4.5.9-alpha 38 12/20/2025
4.5.8 36 12/13/2025
4.5.7 46 12/17/2025
4.5.7-alpha 35 12/13/2025
4.5.6-alpha 34 12/20/2025
4.5.3 53 12/14/2025
4.5.0 41 12/10/2025
4.2.1510.2205 35 12/31/2025
4.2.1507.118 40 12/10/2025
4.2.1506.2515 37 12/13/2025
4.2.1506.2016 34 12/20/2025
4.2.1502.911 36 12/10/2025
4.2.1409.1722 37 12/20/2025
4.2.1408.717 34 12/10/2025
4.2.1408.619 42 12/09/2025
4.2.1402.2112 36 12/20/2025
4.2.1312.1622 37 12/19/2025
4.2.1312.1621 37 12/20/2025
4.2.1312.1615 37 12/20/2025
4.2.1312.1416 35 12/31/2025
4.2.1312.1323 39 12/20/2025
4.2.1312.1319 34 12/12/2025
4.1.1311.615 30 12/09/2025
4.1.1309.1617 36 12/20/2025
4.1.1309.919 38 12/10/2025
4.1.1309.801 37 12/10/2025
4.1.1309.800 35 12/10/2025
4.1.1308.2321 35 12/16/2025
4.1.1308.2316 35 12/18/2025
4.1.1308.2120 49 12/22/2025
4.0.10827 56 12/10/2025
3.1.416.3 33 12/15/2025