使用RhinoMocks模拟视图

时间:2010-03-03 16:31:27

标签: c# .net unit-testing rhino-mocks

我们正在使用带有3.5 SP1的ASP.NET WebForms的MVP(监督控制器)。

检查仅具有RhinoMocks的set操作的view属性的值的首选方法是什么?

这是我们到目前为止所做的:

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

如果我们使用Expects或其他方式,任何输入都会很棒。

由于

根据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

这不起作用。我应该这样测试还是有更好的方法?

1 个答案:

答案 0 :(得分:0)

您可以声明已使用正确的参数调用setter属性上的Bars。假设Bars属性是一个字符串数组:

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

// act
view.Bars = bars;

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

这也应该有效:

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