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. 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. You don't need an instance to the MockRepository anymore (this had been used for Rhino before 3.5). Just call the static methods on 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.
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.
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. You don't need to call Record
explicitly in these cases.
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();
Before the Replay
method is called the RhinoMocks mocks is in the recording state. 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
. Calling Replay
stops your test from changing how the mock behaves and starts to actually behave as you have instructed.
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. I would strongly recommend against this. Typically I just use the MockRepository.ReplayAll()
and MockRepository.VerifyAll()
methods.
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. For the AAA pattern the other syntax should be used:
var stubbedInterface = MockRepository.GenerateStub<IAnInterface>();
stubbedInterface.Stub(x =>SomeProperty).Return(someValue);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With