talira

The most popular and friendly mocking framework for .NET

open sourceBSD-3-Clausefree

Moq is a mocking library for .NET tests. It creates mock objects of interfaces and classes, sets up what their methods and properties return or throw, and verifies which calls were made and how often, using LINQ expression syntax, with Mock.Of for quick stubs. The stable 4.x releases on NuGet are BSD-3-Clause; the main branch holds the upcoming Moq 5, MIT-licensed, which compiles mocks into the test project as source and generates typed setups for C# 14.

Problems it solves

  • mock a dependency in my .NET unit tests without writing a fake class by hand
  • check that my C# code called a service with the right arguments
  • make a mocked interface return a value or throw an exception in an xUnit or NUnit test
  • stub out a repository so my service tests don't hit the database

Features

  • mocks of interfaces and classes
  • setups for return values, exceptions and callbacks
  • call verification with counts
  • LINQ to Mocks with Mock.Of
  • argument matchers
  • events and ref/out parameters
  • Moq 5 typed setups generated as source (C# 14)