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
CheeseDataService
Package to access SIERA+ related data services and models
537
MockQueryable.Moq
Extension for mocking Entity Framework Core operations such ToListAsync, FirstOrDefaultAsync etc. When writing tests for your application it is often desirable to avoid hitting the database. The extension allows you to achieve this by creating a context – with behavior defined by your tests – that makes use of in-memory data.
478
CheeseDataService
Package to access SIERA related data services and models
83
CheeseDataService
Package to access SIERA related data services and models
55
MockQueryable.Moq
Extension for mocking Entity Framework Core operations such ToListAsync, FirstOrDefaultAsync etc. When writing tests for your application it is often desirable to avoid hitting the database. The extension allows you to achieve this by creating a context – with behavior defined by your tests – that makes use of in-memory data.
51
MockQueryable.Moq
Extension for mocking Entity Framework Core operations such ToListAsync, FirstOrDefaultAsync etc. When writing tests for your application it is often desirable to avoid hitting the database. The extension allows you to achieve this by creating a context – with behavior defined by your tests – that makes use of in-memory data.
49
MockQueryable.Moq
Extension for mocking Entity Framework Core operations such ToListAsync, FirstOrDefaultAsync etc. When writing tests for your application it is often desirable to avoid hitting the database. The extension allows you to achieve this by creating a context – with behavior defined by your tests – that makes use of in-memory data.
38
MockQueryable.Moq
Extension for mocking Entity Framework Core operations such ToListAsync, FirstOrDefaultAsync etc. by Moq When writing tests for your application it is often desirable to avoid hitting the database. The extension allows you to achieve this by creating a context – with behavior defined by your tests – that makes use of in-memory data.
36
MockQueryable.Moq
Extension for mocking Entity Framework operations such ToListAsync, FirstOrDefaultAsync etc. by Moq When writing tests for your application it is often desirable to avoid hitting the database. The extension allows you to achieve this by creating a context – with behavior defined by your tests – that makes use of in-memory data.
36
MockQueryable.Moq
Core package for MockQueryable extensions for mocking operations such ToListAsync, FirstOrDefaultAsync etc. When writing tests for your application it is often desirable to avoid hitting the database. The extension allows you to achieve this by creating a context – with behavior defined by your tests – that makes use of in-memory data.
36
MockQueryable.Moq
Extension for mocking Entity Framework Core operations such ToListAsync, FirstOrDefaultAsync etc. by Moq When writing tests for your application it is often desirable to avoid hitting the database. The extension allows you to achieve this by creating a context – with behavior defined by your tests – that makes use of in-memory data.
35
MockQueryable.Moq
Extension for mocking Entity Framework Core operations such ToListAsync, FirstOrDefaultAsync etc. When writing tests for your application it is often desirable to avoid hitting the database. The extension allows you to achieve this by creating a context – with behavior defined by your tests – that makes use of in-memory data.
35
MockQueryable.Moq
Core package for MockQueryable extensions for mocking operations such ToListAsync, FirstOrDefaultAsync etc. When writing tests for your application it is often desirable to avoid hitting the database. The extension allows you to achieve this by creating a context – with behavior defined by your tests – that makes use of in-memory data.
35
MockQueryable.Moq
Extension for mocking Entity Framework Core operations such ToListAsync, FirstOrDefaultAsync etc. When writing tests for your application it is often desirable to avoid hitting the database. The extension allows you to achieve this by creating a context – with behavior defined by your tests – that makes use of in-memory data.
33
MockQueryable.Moq
Extension for mocking Entity Framework Core operations such ToListAsync, FirstOrDefaultAsync etc. by Moq When writing tests for your application it is often desirable to avoid hitting the database. The extension allows you to achieve this by creating a context – with behavior defined by your tests – that makes use of in-memory data.
32
MockQueryable.Moq
Core package for MockQueryable extensions for mocking operations such ToListAsync, FirstOrDefaultAsync etc. When writing tests for your application it is often desirable to avoid hitting the database. The extension allows you to achieve this by creating a context – with behavior defined by your tests – that makes use of in-memory data.
32
MockQueryable.Moq
Extension for mocking Entity Framework Core operations such ToListAsync, FirstOrDefaultAsync etc. by Moq When writing tests for your application it is often desirable to avoid hitting the database. The extension allows you to achieve this by creating a context – with behavior defined by your tests – that makes use of in-memory data.
31
CheeseDataService
Package to access SIERA related data services and models
31

.NET Framework 4.6.2

.NET Standard 2.0

.NET Standard 2.1

.NET 6.0

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