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