CheckOps
NSubstitute
NSubstitute is a BSD-3-Clause mocking library for .NET with a concise, readable syntax for creating substitutes, configuring returns and checking received calls.
Latest public signals
Not refreshed yet. Refresh reads the public site and, when the entry has a GitHub repository, the latest release.
Introduction
NSubstitute is a friendly substitute for .NET mocking libraries. Its design goal is a succinct syntax that lets tests focus on behaviour rather than mocking ceremony: there is no distinction between mocks and stubs, just “substitutes” created with Substitute.For<T>().
You set return values by calling the member and adding .Returns(...) (for example calculator.Add(1, 2).Returns(3)), use argument matchers such as Arg.Any<int>() and Arg.Is<int>(x => x > 0), and check interactions with .Received() or .DidNotReceive(). It supports multiple and sequential returns, callbacks with When(...).Do(...), throwing exceptions, async methods, events, auto-substitution of recursive members and argument capture. A Roslyn analyzer package (NSubstitute.Analyzers) catches common mistakes such as substituting non-virtual members. Version 6.2.0 was released on 11 August 2026.
Because substitutes are configured by calling members directly, NSubstitute tests often read almost like the production code they exercise, which many teams find easier to review than expression-based setups.
The source code is at nsubstitute/NSubstitute on GitHub under the BSD-3-Clause licence, with about 3,000 stars; the most recent commit was on 26 October 2026.
Features
- Concise syntax:
Substitute.For<T>(),.Returns()and.Received(). - Argument matchers:
Arg.AnyandArg.Is. - Async and events: Works naturally with tasks and events.
- Analyzers: Catch misuse at compile time.
Pros
- Free to use: Open source under the BSD-3-Clause licence, so the core tool costs nothing to use.
- Actively maintained: The repository had commits as recent as 26 October 2026.
Cons
- Virtual members only: Like other proxy-based libraries, cannot mock sealed or non-virtual members.
- Implicit setup style: Calling members to configure them can confuse newcomers.
Usage and Links (Example)
var gateway = Substitute.For<IPaymentGateway>();
gateway.ChargeAsync(Arg.Any<string>(), 100).Returns(Task.FromResult("tx-1"));
var checkout = new CheckoutService(gateway);
await checkout.PayAsync("card-1", 100);
await gateway.Received(1).ChargeAsync("card-1", 100);
gateway.DidNotReceive().Refund(Arg.Any<string>());
Resources:
Pricing
NSubstitute is free and open source under the BSD-3-Clause licence.
Recommended for?
- .NET developers who prefer minimal, readable mocking syntax.
- Teams writing behaviour-focused unit tests.
Conclusion
NSubstitute offers the most readable mocking syntax in .NET, a strong alternative to Moq.
Alternatives
Other tools on CheckOps that cover similar ground. Open a page, or compare one with NSubstitute side by side.
MMoqMoq is a popular MIT-licensed mocking library for .NET that uses lambda expressions to set up and verify mocks; stable v4.x is maintained on the v4 branch while the main branch hosts the upcoming v5.Compare with NSubstitute
XxUnit.netxUnit.net is a free, Apache-2.0, community-focused unit testing tool for C#, F# and Visual Basic, part of the .NET Foundation; v3 supports .NET 8+ and .NET Framework 4.7.2+.Compare with NSubstitute
NNUnitNUnit is an MIT-licensed unit-testing framework for all .NET languages on Windows, macOS and Linux, broad and non-opinionated, now at version 5 after NUnit 3 and 4.Compare with NSubstitute
MMSTestMSTest is Microsoft's MIT-licensed test framework for .NET, developed in the microsoft/testfx repository alongside Microsoft.Testing.Platform, a lightweight alternative to VSTest, with the MSTest.Sdk project SDK.Compare with NSubstitute
FFluent AssertionsFluent Assertions is a .NET assertions library with natural-language `Should()` syntax; versions 8 and later are free for open-source and non-commercial use but require a paid licence (via Xceed) for commercial use.Compare with NSubstitute
SShouldlyShouldly is a BSD-3-Clause .NET assertion library focused on readable code and great failure messages that include the source expression being tested.Compare with NSubstitute