如何在返回模拟对象的方法调用结果之前对其进行修改?
给出以下使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试使用 WhenCalled()。 WhenCalled 的参数允许访问模拟方法的参数,您还可以设置返回值。
Try using WhenCalled(). The parameter to WhenCalled allows access to the mocked method's arguments and you can also set the return value.