简体   繁体   English

如何为Rhino Mocked对象设置值?

[英]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.

相关问题 使用Rhino Mocks如何为Mocked方法设置参数的属性 - Using Rhino Mocks how can I set a property of a parameter for a Mocked method 犀牛模拟:如何判断对象是嘲笑还是真实? - Rhino Mocks: How to tell if object is mocked or real? 如何在模拟对象中创建模拟对象? - How can I create a mocked object inside a mocked object? 我可以在Rhino Mocked对象上定义方法的实现吗? - Can I define implementation of methods on Rhino Mocked objects? 如何使用Rhino模拟仅模拟测试对象中的一个值? - How can I use Rhino mocks to mock just one value in a test object? 如何在设置方法中为犀牛模拟界面设置默认操作,在测试方法中可以为特定行为覆盖默认操作? - How can I setup default actions for a rhino mocked interface in a setup method, which can be overridden for specific behavior in a test method? 如何在Rhino Mock&#39;d对象中调用回调? - How can I call a callback in a Rhino Mock'd object? 使用Rhino从模拟对象引发事件 - Raising an event from a mocked object using Rhino Rhino模拟无法对模拟对象的第二次调用设定期望,该对象的接口返回一个Func <T> - Rhino mocks is unable to set an expectation on the 2nd call of mocked object whose interface returns a Func<T> 如何在模拟的容器对象中传递模拟的对象 - How do I pass mocked objects in a mocked container object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM