[英]How can I set a value to a Rhino Mocked object?
I have a scenario like this: 我有这样的情况:
form = MockRepository.GenerateMock<IAddAddressForm>();
mediator = new AddAddressMediator(form);
The mediator is the real object under test and needs to be able to set values for the "form" object. 中介者是测试中的真实对象,需要能够为“ form”对象设置值。
But the only way I can see to set values for the form object is like this: 但是我看到的唯一设置表单对象值的方法是这样的:
form.Stub(x=>x.FirstName).Return(item.FirstName)
I don't want to be doing that in my real code. 我不想在我的真实代码中这样做。
Am I missing the point of mocks? 我错过了模拟的重点吗?
Stubs have built in support for property behaviour. 存根内置了对财产行为的支持。 In cases where you aren't using stubs, you can use the PropertyBehaviour() method for a similar effect.
如果您不使用存根,则可以使用PropertyBehaviour()方法获得类似效果。
Within the mediator, you should be using the form
object normally -- it should not know that it has been handed a fake object. 在调解器中,您应该正常使用
form
对象-它不应该知道它已被传递给假对象。
This code: 这段代码:
form.Stub(x=>x.FirstName).Return(item.FirstName)
should not be in your real object, but may be part of your test to set up the expectations for how you will use your mock object. 不应在您的真实对象中,而应作为测试的一部分,以建立有关如何使用模拟对象的期望。
Edit: 编辑:
From what you've provided, I can't judge whether you're "missing the point of mocks". 根据您提供的信息,我无法判断您是否“缺少模拟的要点”。 The essential purpose is to provide a way to test code that has dependencies in isolation from those dependencies.
基本目的是提供一种测试具有独立于这些依赖关系的依赖关系的代码的方法。 Have a look at Martin Fowler's essay " Mocks Aren't Stubs ", and the Usage Guidance section of the Rhino Mocks documentation .
查看Martin Fowler的文章“ Mocks Are n't Stubs ”,以及Rhino Mocks文档的“使用指南”部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.