Suppose I have a stubbed interface acting as a proxy for the real object:
IDatabase databaseStub = MockRepository.GenerateStub();
Now suppose I have a parameterized method which needs redirecting to some utility method, for instance to return some test data:
DataSet IDatabase.RetrieveData(int entityId);
And I have a local utility method in my test:
DataSet GetTestData(int entityId);
and I wish for this method to be called whenever the RetrieveData method is called on the stub.
I make use of WhenCalled and GetArgumentsForCallsMadeOn as follows:
databaseStub.Stub(x => x.RetrieveData(Arg.Is.Anything))
.WhenCalled((methodInvocation) => {
var args = databaseStub.GetArgumentsForCallsMadeOn(x => x.RetrieveData(Arg.Is.Anything));
int lastCallIndex = args.Count - 1;
methodInvocation.ReturnValue = GetTestData((int)args[lastCallIndex][0]);
}).Return(new Dealer());
The lastCallIndex variable is to capture the most recent addition to the args array, it keeps all arguments throughout the test in this array, so if you are only interested in the most recent call, your arguments array will be the last element in this array.
The return of the blank object at the end is to direct the stub as to the correct return type of the method, otherwise you will receive a run-time exeption : "Method "RetrieveData" requires a return value or an exception to throw."
