简体   繁体   English

当使用Rhino Mocks对函数进行存根时,为什么需要重放()?

[英]Why do I need to Replay() when stubbing a function with Rhino Mocks?

var contextChannel = this.MockRepository.Stub<IContextChannel>();
var context = this.MockRepository.Stub<IOperationContext>();
context.Stub(x => x.Channel).Return(contextChannel);
context.Replay();

What is Replay for? 什么是重播?

I understand that in the case of recording and then playing back an action, the Replay() call is necessary. 我知道在录制和播放动作的情况下,Replay()调用是必要的。 But it is unclear to me why I am forced to write one more line of code in the case where I do not record anything. 但是我不清楚为什么在我没有记录任何内容的情况下我被迫再编写一行代码。 All I need is a property which returns my object. 我需要的只是一个返回我的对象​​的属性。

Update: 更新:

You are not using the AAA syntax properly. 您没有正确使用AAA语法。 You don't need an instance to the MockRepository anymore (this had been used for Rhino before 3.5). 您不再需要MockRepository的实例(这已经在3.5之前用于Rhino)。 Just call the static methods on MockRepository: 只需在MockRepository上调用静态方法:

var contextChannel = MockRepository.GenerateStub<IContextChannel>();
var context = MockRepository.GenerateStub<IOperationContext>();
context.Stub(x => x.Channel).Return(contextChannel);

Here is some documentation: 这是一些文档:

Original Answer 原始答案

You don't. 你没有。 There is not need to call Replay in a situations like yours anymore. 没有必要在像你这样的情况下调用Replay

In previous versions, there was a "record-replay" paradigm, where you recorded expectations and replayed them during the test. 在以前的版本中,有一个“记录重放”范例,您可以记录期望并在测试期间重播它们。 It had been replaced by the AAA syntax, where you can much easier and more flexible set up mocks. 它已被AAA语法所取代,您可以更轻松,更灵活地设置模拟。

Behind the scenes, there is still a record and replay state of the mock. 在幕后,仍然有模拟的记录和重播状态。 Methods like Stub are putting the mock into record state, configure it, and put them back to record. Stub这样的方法将模拟器置于记录状态,配置它,然后将它们放回记录状态。 You don't need to call Record explicitly in these cases. 在这些情况下,您无需显式调用Record

If you want to do some more advanced operations, you can set the mock to replay state yourself, do something with it, eg. 如果你想做一些更高级的操作,你可以自己设置模拟重放状态,用它做一些事情,例如。 in order to reset expectations: 为了重置期望:

mock.BackToRecord(BackToRecordOptions.All);
mock.Replay();

Answer evacuated from question: 回答问题:

var repo = new MockRepository();
var stubbedInterface = repo.Stub<IAnInterface>();
stubbedInterface.Stub(x => x.SomeProperty).Return(someValue);

The last line here puts the repository in the recording state, nevertheless it is a simple stub. 这里的最后一行将存储库置于记录状态,但它是一个简单的存根。 Thus a Replay is necessary. 因此Replay是必要的。 For the AAA pattern the other syntax should be used: 对于AAA模式,应使用其他语法:

var stubbedInterface = MockRepository.GenerateStub<IAnInterface>();
stubbedInterface.Stub(x =>SomeProperty).Return(someValue);

Before the Replay method is called the RhinoMocks mocks is in the recording state. 在调用Replay方法之前,RhinoMocks模拟处于记录状态。 This means you can control how the mock will behave, even though you're not recordnig anything per se, you still telling the mock how to behave isomg for example Stub . 这意味着你可以控制模拟的行为方式,即使你本身没有记录任何东西,你仍然告诉模拟如何表现为 isomg,例如Stub Calling Replay stops your test from changing how the mock behaves and starts to actually behave as you have instructed. 调用Replay阻止您的测试更改模拟的行为方式,并按照您的指示开始实际操作。

UPDATE UPDATE

The Record method exists only to allow tests to move a mock object back to the recording state and modify the behaviour of the mock. Record方法仅用于允许测试将模拟对象移回记录状态并修改模拟的行为。 I would strongly recommend against this. 我强烈建议不要这样做。 Typically I just use the MockRepository.ReplayAll() and MockRepository.VerifyAll() methods. 通常我只使用MockRepository.ReplayAll()MockRepository.VerifyAll()方法。

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

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