What would this Moq code look like in RhinoMocks

蓝咒 提交于 2019-12-21 09:26:41

问题


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);

回答1:


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));



回答2:


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; }));



回答3:


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);


来源:https://stackoverflow.com/questions/1020673/what-would-this-moq-code-look-like-in-rhinomocks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!