Search code examples
c#rhino-mocks

Making RhinoMock Stub Return Value Dependent on a Paramter


Is it possible to set a "default" return value for a RhinoMock stub?

For ex: I have a method that takes in an int from 1-175 and returns a bool:

internal bool myFunction(int number)

I want to stub this in Rhino Mock so that it only returns true if the number is 2 or 3, and false otherwise:

myClass.Stub(x => x.MyFunction(2)).Return(true);
myClass.Stub(x => x.MyFunction(3)).Return(true);
// return false otherwise

However, if I pass any other int in my test (eg. 100), the function will also return true. I want it to return false all cases except the two I listed above. How can I do this?


Solution

  • Use Do to provide a method implementation with access to the input parameters

    myClass.Stub(_ => _.MyFunction(Arg<int>.Is.Anything))
        .Do((Func<int, bool>)(number => {
            return number == 2 || number == 3;
        }));