CheckOps home

Tools directory

N

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.

Visit website

Latest public signals

Not refreshed yet. Refresh reads the public site and, when the entry has a GitHub repository, the latest release.

Visit Website

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

  1. Concise syntax: Substitute.For<T>(), .Returns() and .Received().
  2. Argument matchers: Arg.Any and Arg.Is.
  3. Async and events: Works naturally with tasks and events.
  4. Analyzers: Catch misuse at compile time.

Pros

  1. Free to use: Open source under the BSD-3-Clause licence, so the core tool costs nothing to use.
  2. Actively maintained: The repository had commits as recent as 26 October 2026.

Cons

  1. Virtual members only: Like other proxy-based libraries, cannot mock sealed or non-virtual members.
  2. Implicit setup style: Calling members to configure them can confuse newcomers.
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.

  1. .NET developers who prefer minimal, readable mocking syntax.
  2. 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.