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()的存根调用返回之前,如何修改模型变量中包含的对象?我想在Build()返回之前执行分配。我尝试使用Do()处理程序,但我放弃了......model.States = states

4

1 回答 1

2

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

.WhenCalled(m => {
   Model model = (Model) m.Arguments[0];
   model.States = ...;
});
于 2011-02-24T20:28:34.767 回答