According to Call information documentation
The return value for a call to a property or method can be set to the
result of a function.
var myThing = Substitute.For<IMyThing>()
myThing
.MyMethod(Arg.Any<int>())
.Returns(args => ((int)args[0]) + 1); //<-- Note access to pass arguments
The parameter of the lambda function will give access to the arguments passed to this call at the specified zero-based position.
For strongly typed args the following can also be done.
var myThing = Substitute.For<IMyThing>()
myThing
.MyMethod(Arg.Any<int>())
.Returns(args => args.ArgAt<int>(0) + 1); //<-- Note access to pass arguments
T ArgAt<T>(int position)
: Gets the argument passed to this call at the specified zero-based position, converted to type T
.
And since in this case there is only one parameter it can be simplified even further to
var myThing = Substitute.For<IMyThing>()
myThing
.MyMethod(Arg.Any<int>())
.Returns(args => args.Arg<int>() + 1); //<-- Note access to pass arguments
Here args.Arg<int>()
will return the int
argument passed to the call, rather than having to use (int)args[0]
. If there were more than one then the index would be used.