如何在返回模拟对象的方法调用结果之前对其进行修改?

发布于 2024-10-18 22:51:48 字数 984 浏览 1 评论 0原文

给出以下使用 RhinoMocks 和 MSpec 的简化示例:

[Subject(typeof (LocationController))]
public class when_creating_a_location_with_invalid_model : context_for_location_controller
{
    static LocationModel model = new LocationModel();
    static SelectList states = new SelectList(new Dictionary<string,string> {
        { "IN", "Indiana" }, { "NY", "New York" }
    });

    static ActionResult result;

    Establish context = () =>
        {
            LocationModelBuilder.Stub(x =>
                x.Build(Arg<LocationModel>.Is.Equal(model))).Return(model);
        }

    Because of = () => result = subject.Create(model);

    It should_automatically_select_a_state = () => result.Model<LocationModel>()
         .States.ShouldNotBeEmpty();
}

在从 LocationModelBuilder.Build() 的存根调用返回之前,如何修改 model 变量中包含的对象? ?我想在 Build() 返回之前执行类似 model.States = states 的分配。我尝试使用 Do() 处理程序,但我放弃了......

Given the following streamlined example, using RhinoMocks and MSpec:

[Subject(typeof (LocationController))]
public class when_creating_a_location_with_invalid_model : context_for_location_controller
{
    static LocationModel model = new LocationModel();
    static SelectList states = new SelectList(new Dictionary<string,string> {
        { "IN", "Indiana" }, { "NY", "New York" }
    });

    static ActionResult result;

    Establish context = () =>
        {
            LocationModelBuilder.Stub(x =>
                x.Build(Arg<LocationModel>.Is.Equal(model))).Return(model);
        }

    Because of = () => result = subject.Create(model);

    It should_automatically_select_a_state = () => result.Model<LocationModel>()
         .States.ShouldNotBeEmpty();
}

How can I modify the object contained in the model variable before it is returned from the stubbed call of LocationModelBuilder.Build()? I want to perform an assignment like model.States = states just before return on Build(). I tried playing with the Do() handler but I give up...

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

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

发布评论

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

评论(1

所有深爱都是秘密 2024-10-25 22:51:48

尝试使用 WhenCalled()。 WhenCalled 的参数允许访问模拟方法的参数,您还可以设置返回值。

.WhenCalled(m => {
   Model model = (Model) m.Arguments[0];
   model.States = ...;
});

Try using WhenCalled(). The parameter to WhenCalled allows access to the mocked method's arguments and you can also set the return value.

.WhenCalled(m => {
   Model model = (Model) m.Arguments[0];
   model.States = ...;
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文