Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RhinoMocks - Stub a Method That Returns a Parameter

I am using RhinoMocks, I need to stub a method, and always have it return the third parameter, regardless of what is passed in:

_service.Stub(x => x.Method(parm1, parm2, parm3)).Return(parm3);

Obviously, it ain't that easy. I don't always know what the parms are going to be, but I know I always want to return the 3rd one.

like image 894
Martin Avatar asked Nov 12 '09 15:11

Martin


2 Answers

You can provide an implementation for a method with the Do() handler:

Func<TypeX,TypeY,TypeZ,TypeZ> returnThird = (x,y,z) => z;
mock.Expect(x => x.Method(null, null, null)).IgnoreArguments().Do(returnThird);

Note that TypeZ appears twice because it is both an input argument type and the return type.

like image 68
Wim Coenen Avatar answered Sep 17 '22 10:09

Wim Coenen


This worked for me:

        _service
            .Stub(x => x.Method(Arg<string>.Is.Anything, ... ))
            .Return(null) // ... or default(T): will be ignored but RhinoMock requires it
            .WhenCalled(x =>
            {
                // This will be used as the return value
                x.ReturnValue = (string) x.Arguments[0];
            });
like image 7
Jon Rea Avatar answered Sep 17 '22 10:09

Jon Rea