Returning Different Results for Sequential Calls
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()
.
Last updated