Deferred returns - hypersolutions/hypermock GitHub Wiki
Setup of a deferred return value
There are situations where you may not know the value to be returned whilst configuring the setup of a function or property.
In these situations, the Returns method is overloaded to accept a Func(Of T) input, where T is the return type of the function or property:
[TestMethod]
public void SetupReturnsDeferredValue()
{
var canDebit = false;
var info = new AccountInfo { Number = "12345678", DebitAmount = 100 };
MockFor<IAccountService>().Setup(s => s.CanDebit(info.Number, info.DebitAmount)).Returns(() => canDebit);
canDebit = true;
Subject.Debit(info);
MockFor<IAccountService>().Verify(s => s.Debit(info.Number, info.DebitAmount), Occurred.Once());
}
From the above you can see that the mock of CanDebit is configured to accept an anonymous function and, later the canDebit variable is set to true.
The evaluation of the return function occurs at the point of which a matching setup is found and invoked, allowing control of the return value before the call is to be made.