RhinoMocks - 存根返回参数的方法

发布于 2024-08-10 23:02:04 字数 197 浏览 3 评论 0原文

我正在使用RhinoMocks,我需要存根一个方法,并始终让它返回第三个参数,无论传入什么:

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

显然,这并不那么容易。我并不总是知道参数会是什么,但我知道我总是想返回第三个参数。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

や莫失莫忘 2024-08-17 23:02:05

您可以使用 Do() 处理程序

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

请注意,TypeZ 出现两次,因为它既是输入参数类型又是返回类型。

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.

爱的故事 2024-08-17 23:02:05

这对我有用:

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

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];
            });
囍笑 2024-08-17 23:02:05

您可以使用带有回调的 Expect 方法来返回您想要的值。以下将返回 null。

_service.Expect(o => o.Method(null, null, null))
        .Callback((object parm1, object parm2, object parm3) => { return parm3; });

我不确定您是否可以在存根上使用回调。

You could use the expect method with a callback to return the value that you are after. The following will return null.

_service.Expect(o => o.Method(null, null, null))
        .Callback((object parm1, object parm2, object parm3) => { return parm3; });

I am not sure if you can use Callback on Stub.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文