- 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
Return for any args
A call can be configured to return a value regardless of the arguments passed using the ReturnsForAnyArgs()
extension method.
calculator.Add(1, 2).ReturnsForAnyArgs(100);
Assert.AreEqual(100, calculator.Add(1, 2));
Assert.AreEqual(100, calculator.Add(-7, 15));
Tip! You can also use the default
C# keyword for better readability:
calculator.Add(default, default).ReturnsForAnyArgs(100);
The same behaviour can also be achieved using argument matchers: it is simply a shortcut for replacing each argument with Arg.Any<T>()
.
ReturnsForAnyArgs()
has the same overloads as Returns()
, so you can also specify multiple return values or calculated return values using this approach.