views:

314

answers:

3

Hey people... trying to get my mocking sorted with asp.net MVC.

I've found this example on the net using Moq, basically I'm understanding it to say: when ApplyAppPathModifier is called, return the value that was passed to it.

I cant figure out how to do this in Rhino Mocks, any thoughts?

var response = new Mock<HttpResponseBase>();
response.Expect(res => res.ApplyAppPathModifier(It.IsAny<string>()))
            .Returns((string virtualPath) => virtualPath);
A: 

Unless I'm misreading the code, I think you can simplify that down quite a bit. Try this:

var response = MockRepository.GenerateMock<HttpResponseBase>();

response.Stub(res => res.ApplyAppPathModifier(Arg<String>.Is.Anything)) 
                        .IgnoreArguments()
                        .Return(virtualPath);
womp
@womp, thanks mate. In your example what is virtualPath, an existing variable? What I'm after is to return the variable that was actually passed to ApplyAppPathModifier. This isn't know till runtime. I actually got it to work using my comment above and will post it now
Keith
If you want to know the arguments passed to ApplyAppPathModifier, you can use the code of womp and add the following line: var args = response.GetArgumentsForCallsMadeOn(x => x.ApplyAppPathModifier(Arg<String>.Is.Anything));
Francis B.
+1  A: 

As I mentioned above, sods law, once you post for help you find it 5 min later (even after searching for a while). Anyway for the benefit of others this works:

SetupResult
    .For<string>(response.ApplyAppPathModifier(Arg<String>.Is.Anything)).IgnoreArguments()
    .Do((Func<string, string>)((arg) => { return arg; }));
Keith
+2  A: 

If you are using the stub method as opposed to the SetupResult method, then the syntax for this is below:

response.Stub(res => res.ApplyAppPathModifier(Arg<String>.Is.Anything))
                .Do(new Func<string, string>(s => s));
DownChapel