- 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 specific args
Return values can be configured for different combinations of arguments passed to calls using argument matchers. This topic is covered in more detail in the Argument matchers entry, but the following examples show the general idea.
//Return when first arg is anything and second arg is 5:
calculator.Add(Arg.Any<int>(), 5).Returns(10);
Assert.AreEqual(10, calculator.Add(123, 5));
Assert.AreEqual(10, calculator.Add(-9, 5));
Assert.AreNotEqual(10, calculator.Add(-9, -9));
//Return when first arg is 1 and second arg less than 0:
calculator.Add(1, Arg.Is<int>(x => x < 0)).Returns(345);
Assert.AreEqual(345, calculator.Add(1, -2));
Assert.AreNotEqual(345, calculator.Add(1, 2));
//Return when both args equal to 0:
calculator.Add(Arg.Is(0), Arg.Is(0)).Returns(99);
Assert.AreEqual(99, calculator.Add(0, 0));