简体   繁体   English

用RhinoMocks模拟视图

[英]Mocking View with RhinoMocks

We are using MVP (Supervising Controller) for ASP.NET WebForms with 3.5 SP1. 我们正在对带有3.5 SP1的ASP.NET WebForms使用MVP(监督控制器)。

What is the preferred way to check the value of a view property that only has the a set operation with RhinoMocks? 检查RhinoMocks仅具有set操作的view属性值的首选方法是什么?

Here is what we have so far: 到目前为止,这里是:

var service = MockRepository.GenerateStub<IFooService>();
// stub some data for the method used in OnLoad in the presenter

var view = MockRepository.GenerateMock<IFooView>();
var presenter = new FooPresenter(view, service);

view.Raise(v => v.Load += null, this, EventArgs.Empty);

Assert.IsTrue(view.Bars.Count == 10); // there is no get on Bars

Should we use Expects or another way, any input would be great. 如果我们使用Expects或其他方式,那么任何输入都会很棒。

Thanks 谢谢

Update based on Darin Dimitrov's reply. 根据Darin Dimitrov的回复进行更新。

var bars = new List<Bar>() { new Bar() { BarId = 1 } };

var fooService = MockRepository.GenerateStub<IFooService>();

// this is called in OnLoad in the Presenter
fooService.Stub(x => x.GetBars()).Return(bars);

var view = MockRepository.GenerateMock<IFooView>();
var presenter = new FooPresenter(view, fooService);

view.Raise(v => v.Load += null, this, EventArgs.Empty);
view.AssertWasCalled(x => x.Bars = bars); // this does not pass

This doesn't work. 这行不通。 Should I be testing it this way or is there a better way? 我应该以这种方式进行测试还是有更好的方法?

You could assert that the setter on the Bars property has been called with a correct argument. 您可以断言Bars属性上的setter已使用正确的参数调用。 Assuming the Bars property is an array of strings: 假设Bars属性是一个字符串数组:

// arrange
var view = MockRepository.GenerateMock<IFooView>();
var bars = new[] { "bars" };

// act
view.Bars = bars;

// assert
view.AssertWasCalled(
    x => { x.Bars = bars; }
);

This should also work: 这也应该起作用:

view.AssertWasCalled(
    x => { x.Bars = new[] { "abc" }; }
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM