RhinoMock 模拟可以保存属性值吗?
我已经被困在这个问题上一两天了,我最近开始使用RhinoMocks(v3.5)并且我已经设置了一个测试。一个存根 Web 服务,返回一个 List 集合和一个调用它的类,以及一个模拟对象,该对象具有我希望作为调用 Web 服务的结果而设置的属性。我的代码是这样的:
[Test]
public void Call_WebService_list_populated()
{
IData stService = MockRepository.GenerateStub<IData>();
IDefault mockView = MockRepository.GenerateMock<IDefault>();
DefaultPresenter presenter = new DefaultPresenter(mockView);
presenter.StService = stService;
mockView.Stub(x => x.RequestingUser).Return("test");
List<string> testList = new List<string> { new string() };
stService.Stub(x => x.GetList("test")).Return(testList);
presenter.LoadList();
Assert.AreEqual(testList,mockView.List);
}
在LoadList函数中,它只是分配从web服务返回的列表的mockView的List属性。我可以使用这一行让测试工作:
mockView.AssertWasCalled(a => a.StoryListing = testList);
但我预计模拟对象将保持状态,并且我可以直接检查属性。我是否做错了什么,或者这就是您必须使用 rhino 模拟的方式,即:模拟对象无法保持状态,因为当我执行assert.areequal nunit 时,mockView.List 属性为空。
I have been stuck on this for a day or two, I have recently started using RhinoMocks (v3.5)and I have setup a test. A stub web service that returns a List collection and a class that calls it, and a mock object with a property i expect to be set as a result of the call to the web service. My code is like this:
[Test]
public void Call_WebService_list_populated()
{
IData stService = MockRepository.GenerateStub<IData>();
IDefault mockView = MockRepository.GenerateMock<IDefault>();
DefaultPresenter presenter = new DefaultPresenter(mockView);
presenter.StService = stService;
mockView.Stub(x => x.RequestingUser).Return("test");
List<string> testList = new List<string> { new string() };
stService.Stub(x => x.GetList("test")).Return(testList);
presenter.LoadList();
Assert.AreEqual(testList,mockView.List);
}
In the LoadList function it just assigns the List property of mockView the list returned from the webservice. I can get the test to work using this line:
mockView.AssertWasCalled(a => a.StoryListing = testList);
but i expected that the mock object would hold state and i could check the property directly. Am i doing something wrong or is this just the way you have to use rhino mocks ie: the mock object cant hold state as when i do the assert.areequal nunit says the mockView.List property is null.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
默认情况下,模拟不处理获取/设置属性(不知道为什么。有一种方法可以更改它,但我一时想不起来)。您可以将mockView生成为存根(
MockRepository.GenerateStub()
)——并且存根支持属性行为。By default, mocks don't handle get/set properties (not sure why. There's a way to change it but I can't remember offhand). You can generate your mockView as a stub (
MockRepository.GenerateStub<IDefault>()
) -- and stubs support property behavior.