Moq 4.20.69

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 C. Augusto Proiete Kirill Osenkov MFB Technologies, Inc. Stephen Shaw Torutek DRIVE.NET, Inc. David Kean Daniel Gnägi Ashley Medway Keith Pickford bitbonk Thomas Bolon Yurii Rashkovskii Kori Francis Zdenek Havlin Sean Killeen Toni Wenzel Giorgi Dalakishvili Kelly White Allan Ritchie Mike James Uno Platform Dan Siegel Reuben Swartz Jeremy Simmons Jacob Foshee Eric Johnson Norman Mackay Certify The Web Taylor Mansfield Mårten Rånge David Petric Rich Lee Danilo Dantas Gary Woodfine Steve Bilogan Ix Technologies B.V. New Relic Chris Johnston‮ David JENNI Jonathan Oleg Kyrylchuk Juan Blanco LosManos Mariusz Kogut Charley Wu Thomas Due Jakob Tikjøb Andersen

Sponsor this project  

Learn more about GitHub Sponsors

No packages depend on Moq.

https://github.com/moq/moq/blob/main/changelog.md

.NET Framework 4.6.2

.NET Standard 2.0

.NET Standard 2.1

.NET 6.0

Version Downloads Last updated
4.20.72 12 19.03.2025
4.20.70 11 19.03.2025
4.20.69 11 19.03.2025
4.18.4 12 24.11.2024
4.18.3 11 19.03.2025
4.18.2 11 19.03.2025
4.18.1 11 19.03.2025
4.18.0 11 19.03.2025
4.17.2 12 19.03.2025
4.17.1 11 19.03.2025
4.16.1 12 19.03.2025
4.16.0 11 19.03.2025
4.15.2 11 19.03.2025
4.15.1 11 19.03.2025
4.14.7 11 19.03.2025
4.14.6 11 19.03.2025
4.14.5 11 19.03.2025
4.14.4 11 19.03.2025
4.14.3 11 19.03.2025
4.14.2 11 19.03.2025
4.14.1 10 19.03.2025
4.14.0 11 19.03.2025
4.13.1 11 19.03.2025
4.13.0 11 19.03.2025
4.12.0 11 19.03.2025
4.11.0 11 19.03.2025
4.11.0-rc2 11 19.03.2025
4.11.0-rc1 11 19.03.2025
4.10.1 11 19.03.2025
4.10.0 11 19.03.2025
4.9.0 11 19.03.2025
4.8.3 11 19.03.2025
4.8.2 11 19.03.2025
4.8.1 11 19.03.2025
4.8.0 11 19.03.2025
4.8.0-rc1 11 19.03.2025
4.7.145 11 19.03.2025
4.7.142 11 19.03.2025
4.7.137 12 19.03.2025
4.7.127 11 19.03.2025
4.7.99 11 19.03.2025
4.7.63 11 19.03.2025
4.7.58 11 19.03.2025
4.7.49 11 19.03.2025
4.7.46 11 19.03.2025
4.7.25 11 19.03.2025
4.7.12 11 19.03.2025
4.7.11 11 19.03.2025
4.7.10 12 19.03.2025
4.7.9 11 19.03.2025
4.7.8 11 19.03.2025
4.7.7 11 19.03.2025
4.7.1 11 19.03.2025
4.7.0 11 19.03.2025
4.6.62-alpha 11 19.03.2025
4.6.39-alpha 12 19.03.2025
4.6.38-alpha 11 19.03.2025
4.6.36-alpha 11 19.03.2025
4.6.25-alpha 11 19.03.2025
4.5.30 11 19.03.2025
4.5.29 11 19.03.2025
4.5.28 11 19.03.2025
4.5.23 11 19.03.2025
4.5.22 11 19.03.2025
4.5.21 11 19.03.2025
4.5.20 11 19.03.2025
4.5.19 11 19.03.2025
4.5.18 11 19.03.2025
4.5.16 11 19.03.2025
4.5.13 11 19.03.2025
4.5.10 11 19.03.2025
4.5.9 11 19.03.2025
4.5.9-alpha 10 19.03.2025
4.5.8 11 19.03.2025
4.5.7 11 19.03.2025
4.5.7-alpha 10 19.03.2025
4.5.6-alpha 10 19.03.2025
4.5.3 11 19.03.2025
4.5.0 11 19.03.2025
4.2.1510.2205 11 19.03.2025
4.2.1507.118 11 19.03.2025
4.2.1506.2515 11 19.03.2025
4.2.1506.2016 10 19.03.2025
4.2.1502.911 11 19.03.2025
4.2.1409.1722 11 19.03.2025
4.2.1408.717 11 19.03.2025
4.2.1408.619 11 19.03.2025
4.2.1402.2112 11 19.03.2025
4.2.1312.1622 11 19.03.2025
4.2.1312.1621 11 19.03.2025
4.2.1312.1615 11 19.03.2025
4.2.1312.1416 11 19.03.2025
4.2.1312.1323 11 19.03.2025
4.2.1312.1319 12 19.03.2025
4.1.1311.615 11 19.03.2025
4.1.1309.1617 10 19.03.2025
4.1.1309.919 11 19.03.2025
4.1.1309.801 11 19.03.2025
4.1.1309.800 11 19.03.2025
4.1.1308.2321 11 19.03.2025
4.1.1308.2316 11 19.03.2025
4.1.1308.2120 11 19.03.2025
4.0.10827 11 19.03.2025
3.1.416.3 11 19.03.2025