Overview [ Documentation]

Return Values

Assuming this interface:

public interface ICalculator {
	int Add(int a, int b);
	string Mode { get; set; }
}

And this substitute:

var calculator = Substitute.For<ICalculator>();

Use the following API to set return values:

For Methods

calculator.Add(1, 2).Returns(3);

Using Argument Matching

// 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));

For Any Argument

The ReturnsForAnyArgs method is a shortcut for replacing each argument with Arg.Any<T>():

calculator.Add(1, 2).ReturnsForAnyArgs(100); 
// or...
calculator.Add(default, default).ReturnsForAnyArgs(100);

Assert.AreEqual(100, calculator.Add(1, 2));
Assert.AreEqual(100, calculator.Add(-7, 15));

For Properties

calculator.Mode.Returns("DEC");

For Multiple Return Values (Properties or Methods)

calculator.Mode.Returns("DEC", "HEX", "BIN");

// All true:
Assert.AreEqual("DEC", calculator.Mode);
Assert.AreEqual("HEX", calculator.Mode);
Assert.AreEqual("BIN", calculator.Mode);

If this Assert statement were added, it would fail:

Assert.AreEqual("DEC", calculator.Mode); // Expected "DEC", got "BIN"