- Getting started
- Creating a substitute
- Setting a return value
- Return for specific args
- Return for any args
- Return from a function
- Multiple return values
- Replacing return values
- Checking received calls
- Clearing received calls
- Argument matchers
- Callbacks, void calls and When..Do
- Throwing exceptions
- Safe configuration and overlapping calls
- Raising events
- Auto and recursive mocks
- Setting out and ref args
- Actions with argument matchers
- Checking call order
- Partial subs and test spies
- Return for all calls of a type
- Threading
- Compatibility argument matchers
- NSubstitute.Analyzers
- How NSubstitute works
- Search
NSubstitute.Analyzers
The NSubstitute.Analyzers project uses Roslyn to add code analysis during compilation to detect possible errors using the NSubstitute API.
For example: as noted in the warning in Creating a substitute, non-virtual members of a class can not be intercepted by NSubstitute. This can cause our tests to behave unpredictably if we attempt to use .Returns()
or .Received()
with them. NSubstitute.Analyzers can detect attempts to substitute for non-virtual members and raise an NS1000 warning at compile-time, including suggestions on how to fix it. It’s much nicer to find these problems at compile-time than during test execution, or worse, not finding them until much later when we realise our tests were not doing exactly what we thought they were!
The NSubstitute NuGet package will work fine without any additional packages, but we highly recommend also adding the appropriate Analyzers package to your project (C# or VB):
- NSubstitute.Analyzers.CSharp for C# projects
- NSubstitute.Analyzers.VisualBasic for VB projects
Diagnostics
See NSubstitute.Analyzers documentation for a list of the potential issues NSubstitute.Analyzers can help detect.
Spelling
The Australian members of our team would like to point out they would have preferred to use the English spelling NSubstitute.Analysers
for this project, but have begrudgingly agreed to use the American (mis)spelling instead. :)