- 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
Multiple return values
A call can also be configured to return a different value over multiple calls. The following example shows this for a call to a property, but it works the same way for method calls.
calculator.Mode.Returns("DEC", "HEX", "BIN");
Assert.AreEqual("DEC", calculator.Mode);
Assert.AreEqual("HEX", calculator.Mode);
Assert.AreEqual("BIN", calculator.Mode);
This can also be achieved by returning from a function, but passing multiple values to Returns()
is simpler and reads better.
Multiple returns using callbacks
Returns()
also supports passing multiple functions to return from, which allows one call in a sequence to throw an exception or perform some other action.
calculator.Mode.Returns(x => "DEC", x => "HEX", x => { throw new Exception(); });
Assert.AreEqual("DEC", calculator.Mode);
Assert.AreEqual("HEX", calculator.Mode);
Assert.Throws<Exception>(() => { var result = calculator.Mode; });
Configuring other calls without using up multiple returns
If a call has been configured with multiple returns values, you can configure a more specific call without using up any of these callbacks using .Configure()
.