Moq v5 on main: generated typed setups, mocks compiled as source, and a separate v4 branch
The most popular and friendly mocking framework for .NET
At a glance
- What is it?
- devlooped/moq is the .NET mocking framework whose main branch now holds the upcoming v5, with per-member typed setups that need C# 14 and mock types compiled into your test project, while every tagged release is still a 4.x build.
- Who is it for?
- Teams already on Moq 4.x should keep following the v4 branch and the v4.21.0 release, and treat main as a preview of v5 rather than documentation for the package they reference. Moq v5 suits projects that can move to C# 14 and accept mock types compiled into the test project as source; projects pinned to older language versions will write every setup as a lambda.
- Can I use it commercially?
- Yes. MIT is a permissive licence: you can use, modify and sell software built on it, as long as you keep its copyright and licence notices.
- Is it still maintained?
- Yes. The repository last received commits 3 days ago.
- What is it written in?
- Mainly C#, according to GitHub's language statistics.
Answers come from the project's GitHub data, last synced on November 15, 2026, and from our analysis. They are not legal advice.
Star history
- devlooped/moq6,411 ★ · +6
| Repository | 2026-09-23 | 2026-11-15 |
|---|---|---|
| devlooped/moq | 6.4k | 6.4k |
6 Stars in the last 53 days, measured from the daily GitHub snapshot. Compare with other projects
Editorial analysis
The main branch is Moq v5, not the 4.x package you install from a release
Clone devlooped/moq and open main, and you are reading the upcoming version of Moq, v5. A notice at the top of the readme says so directly and sends readers to the v4 branch for the source and license of the current stable Moq v4.x. The tagged releases confirm the split: the three most recent are v4.21.0 on 24 September 2026, v4.20.72 on 7 September 2024 and v4.20.71 on 3 September 2024. Every one of them is a 4.x build, and v4.21.0 arrived about two years after v4.20.72.
The consequence for a reader is concrete. If your test project references a 4.x package and you browse main to understand a behavior, you are reading code for a version that has no tagged release yet, not the code your tests run against. Source reading, bug investigation and license review for 4.x all belong on the v4 branch. The repository itself is not dormant: the most recent push landed on 27 October 2026, so main keeps moving while the stable line ships from elsewhere.
Typed setups are generated for every member of the mocked type
The v5 usage sample replaces the familiar expression-based setup style with generated members. Each member of the mocked type gets a typed setup on the mock itself, so a method on ICalculator becomes a call you make directly on the Mock<ICalculator> instance:
using Moq;
using static Moq.Syntax;
var calc = new Mock<ICalculator>(); // or Mock.Get(Mock.Of<ICalculator>())
// Typed setups are generated for every mocked type's members (C# 14+).
calc.Add(2, 3).Returns(5);
calc.Add(Any<int>(), Any<int>()).Returns((x, y) => x + y);
calc.Mode.Returns(CalculatorMode.Scientific);
calc.TurnOn().Throws(new InvalidOperationException());
calc.RaiseTurnedOn();Each line shows a different kind of setup. calc.Add(2, 3).Returns(5) fixes a return value for exact arguments. calc.Add(Any<int>(), Any<int>()) matches any pair of integers and computes the result from a lambda that receives both arguments. Any<int>() comes from the using static Moq.Syntax import, which is why that second using line is part of the sample. calc.Mode.Returns(...) configures a property, calc.TurnOn().Throws(...) makes a method throw an InvalidOperationException, and the sample ends with calc.RaiseTurnedOn(), a generated raise call. The mock itself can be created with new Mock<ICalculator>() or by wrapping a plain instance with Mock.Get(Mock.Of<ICalculator>()), so both creation styles reach the same typed surface.
Older C# versions and colliding member names fall back to a setup lambda
The generated syntax carries a hard condition: the comment in the sample marks typed setups as C# 14+. A test project pinned to an earlier language version cannot write calc.Add(2, 3) as a setup at all. The same restriction hits members whose names collide with the mock's own members, and the sample names Object and CallBase as examples. If an interface declared a property called Object, the generated member would clash with the mock's Object, so the typed form is unavailable for it. Recursive setups fall into the same bucket.
The fallback is the Setup function imported from Moq.Syntax: you invoke the mocked object inside a setup lambda, written as Setup(() => ...). That form works on any language version. The cost for a team is two styles in one test suite. Projects that cannot move to C# 14 write every setup as a lambda and get none of the generated surface. Projects that can move still need the lambda form for colliding and recursive cases, so reviewers have to recognize both and know which rule pushed a given test into the lambda form.
Mock<T> is compiled into your test project, and you can extend it with a partial class
In v5, Mock<T> and Mock.Of<T> are compiled into your test project as source rather than consumed only as types from a referenced assembly. Because Mock<T> is a partial class in the Moq namespace, you can add your own partial declaration and hook into how every mock is created:
namespace Moq;
partial class Mock<T>
{
partial void OnCreated() => this.Sdk.Name = typeof(T).Name;
}Here the partial method OnCreated runs when a mock is created and sets this.Sdk.Name to the name of the mocked type. A customization written this way applies to every mock in that test project, with no factory method or wrapper class that tests must remember to call.
The trade is that your project now contains code inside namespace Moq that depends on the partial members Moq declares, such as OnCreated and Sdk. That file is tied to the shape of the generated source, so it is one of the first places to recheck when you move to a newer v5 build. It also means each test project carries its own copy of the mock types, so a customization in one project does not reach another unless you add the same partial class there.
mock.Sdk exposes invocations, setups, state and behaviors
Below the generated surface sits a lower-level runtime reachable from any mock through mock.Sdk. It covers four areas: the invocations recorded on the mock, the setups configured on it, its state, and its behaviors. The OnCreated example already reaches into it by writing Sdk.Name. A separate page, docs/MoqSdk.md, covers this layer, and it sits in the docs/ folder at the root of the repository next to src/, assets/ and the Moq.slnx solution file.
For test authors the split has a practical meaning. The typed setups handle the everyday work of returning values, throwing and raising. When a test needs to look at what was actually called, or a library author wants to build helpers on top of Moq, the entry point is Sdk rather than the generated members. The usage sample only touches Sdk.Name, so anyone planning to build tooling on this layer should read docs/MoqSdk.md before depending on it, and should expect that layer to change along with the rest of v5 until it ships as a tagged release.
Packages built from a clone always report version 42.42.42
For v5 builds, the readme points to a CI package feed at https://pkg.kzu.app/index.json, and to building packages yourself. To build, run the build from the command line or use Pack on the Moq.Package project from the context menu. Packages land in the bin folder at the repository root. You consume them by adding a package source that points to that folder, for example with a NuGet.Config placed anywhere above the directory that holds your test solution:
<configuration>
<packageSources>
<add key="moq" value="[cloned repo dir]\bin" />
</packageSources>
</configuration>The alternative is the RestoreSources property in a project file, or in a Directory.Build.props to affect an entire folder hierarchy. That variant keeps https://api.nuget.org/v3/index.json in the list and adds the clone's bin folder only when it exists, through a Condition on Exists.
Two details change how you work with these builds. Every time packages are produced, the local NuGet cache is cleared, so a later restore in Visual Studio unpacks the updated build again. And a locally built package always carries version 42.42.42. The version number therefore tells you nothing about which commit you built. Two local builds look identical to NuGet, so the only record of what you are testing is the state of your clone when you packed it.
The license you need depends on which branch your package came from
The repository metadata lists MIT, and license.txt sits at the root of main. But the v4 notice sends readers to the v4 branch for the source and license of stable Moq 4.x, so the license covering any 4.x package is the one on that branch, not the file on main. The root of main also holds a file named osmfeula.txt next to license.txt.
For a company with license review, that means two reads, not one. Read license.txt and osmfeula.txt on main if you plan to adopt v5 packages from the CI feed or a local build, and read the license on the v4 branch for any 4.x package your tests reference today. Skipping this step and citing the MIT label alone leaves the second file on main unexamined.
The project is funded through GitHub Sponsors under the devlooped account, with the sponsor list rendered at the bottom of the readme, and it links a Discord server for discussion. Those are the channels to use when a question about v5 behavior is not answered in docs/.
Editorial conclusion
Teams already on Moq 4.x should keep following the v4 branch and the v4.21.0 release, and treat main as a preview of v5 rather than documentation for the package they reference. Moq v5 suits projects that can move to C# 14 and accept mock types compiled into the test project as source; projects pinned to older language versions will write every setup as a lambda. Before adopting either line, check which branch's license covers the package you use, and do not leave a 42.42.42 local build sitting in a restore source.
Frequently asked questions
Is MOQ safe to use?
Moq is listed as MIT licensed, the license for stable 4.x lives on the v4 branch, and main also carries license.txt and osmfeula.txt. The latest release, v4.21.0, was tagged on 24 September 2026, and the repository received a push on 27 October 2026.
Is MOQ a testing framework?
Moq describes itself as a mocking framework for .NET. It creates mock objects such as new Mock<ICalculator>() for use in tests, and in v5 Mock<T> and Mock.Of<T> are compiled into your test project as source.
What are the disadvantages of MOQ?
The main branch holds the upcoming v5 while stable 4.x source is on the v4 branch, so main does not describe the package you get from a 4.x release. In v5, typed setups need C# 14 or later, and older language versions or members named like Object and CallBase need the Setup lambda form.
Official sources
Was this useful?