delphidelphi-mocks

DelphiMocks: Is there any way for a When clause to match every possible input?


I'm trying to setup a mock function that will return a value which is based on the input. The only way to access the input parameter that I know of is via the WillExecute method. However, you have to specify a When clause, and that When clause expects me to define an input value along with the method, in the following fashion:

aMock.Setup.WillExecute(function ...).When.myFunc(1);

I'm kinda forced to say: call that anonymous function, whenever myFunc(1) is called. I'd like to be able to do the same, but on every possible parameter to myFunc, with a kind of wildcard marker in the parameter to myFunc (conceptually):

aMock.Setup.WillExecute(function ...).When.myFunc(*);

Is something like this possible? Basically a When clause that will match any value passed as parameter.

Someone might be tempted to point out the WillReturnDefault value, but method does not have access to the actual parameters of the call, as WillExecute does, so I won't be able to setup anything but a constant value.

Thanks.


Solution

  • Ok, I missed the fact that there was an overloaded version of WillExecute that will do exactly that:

    //Will exedute the func when called with the specified parameters
    function WillExecute(const func : TExecuteFunc) : IWhen<T>;overload;
    
    //will always execute the func no matter what parameters are specified.
    procedure WillExecute(const AMethodName : string; const func : TExecuteFunc);overload;
    

    This way I can tell the mock to execute the passed anon whenever the method is called, regardless of its parameters, while still providing access to them. Exactly what I was looking for. Closing question. Thanks.