A friendly .NET mocking library with a succinct syntax that keeps tests focused on intention rather than configuration.
NSubstitute is a mocking library for .NET that helps developers create test doubles (substitutes) for dependencies in unit tests. It solves the problem of complex and verbose mocking configurations by offering a succinct, readable syntax that keeps tests focused on behavior rather than setup. It supports interfaces and virtual members, making it ideal for isolating and testing components in .NET applications.
.NET developers writing unit tests, especially those new to testing or looking for a more expressive alternative to traditional mocking libraries. It's also suitable for teams aiming to improve test readability and maintainability.
Developers choose NSubstitute for its friendly, natural language syntax that reduces boilerplate and cognitive load in tests. Unlike other mocking libraries, it emphasizes simplicity for common cases while remaining powerful for advanced scenarios, making it easier to write and understand tests.
A friendly substitute for .NET mocking libraries.
Open-Awesome is built by the community, for the community. Submit a project, suggest an awesome list, or help improve the catalog on GitHub.
Uses natural language patterns, as shown in examples like _calculator.Add(1, 2).Returns(3), making test code less cluttered and more expressive than traditional mocking libraries.
Supports Arg.Any<T>() and Arg.Is<T>(predicate) for precise control, allowing tests to verify calls with dynamic or conditional arguments without complex setup.
Provides Received() and DidNotReceive() methods with descriptive error messages, as demonstrated in the call verification example that lists non-matching arguments.
Allows setting return values, sequences, and behaviors using Returns(), including lambdas for computed returns, reducing boilerplate in test setups.
Cannot mock non-virtual methods of classes, as warned in the README, restricting its use in legacy or tightly-coupled codebases without refactoring.
Raising events requires verbose syntax, with the README noting that 'C# dramatically restricts the extent to which this syntax can be cleaned up,' making it less intuitive.
The flexibility in argument matching and dynamic returns can lead to overly complex test setups if misused, undermining the goal of keeping tests simple and focused.
NSubstitute is an open-source alternative to the following products: