NSubstitute
NSubstitute is a friendly substitute for .NET mocking libraries. It has a simple, succinct syntax to help developers write clearer tests. NSubstitute is designed for Arrange-Act-Assert (AAA) testing and with Test Driven Development (TDD) in mind.
Install
| Product | Versions |
|---|---|
| .NET | |
| .NET Standard |
README
NSubstitute
A friendly substitute for .NET mocking libraries.
Perfect for those new to testing, and for others who would just like to to get their tests written with less noise and fewer lambdas.
How to use
// Create:
var calculator = Substitute.For<ICalculator>();
// Set a return value:
calculator.Add(1, 2).Returns(3);
Assert.AreEqual(3, calculator.Add(1, 2));
// Check received calls:
calculator.Received().Add(1, Arg.Any<int>());
calculator.DidNotReceive().Add(2, 2);
// Raise events
calculator.PoweringUp += Raise.Event();
Optional Roslyn analysers
- For C# projects: NSubstitute.Analyzers.CSharp
- For VB projects: NSubstitute.Analyzers.VisualBasic
Getting help
If you have questions, feature requests or feedback on NSubstitute please raise an issue on our project site.
More information
- Visit NSubstitute website for more examples and documentation.
- Visit NSubstitute on GitHub.
Changes from Previous Version
Adoption Assessment
A verified publisher signal is available. Assess fit from the package's supported frameworks, license and dependency graph rather than popularity alone.
Related Guides & Workflows
Learn how to evaluate, integrate, or audit this package in your .NET project.