Why do I need to replay () when executing 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 reproduction?

I understand that in the case of recording and then playing back an action, you must call the Replay () call. But I don’t understand why I am forced to write another line of code in the case when I do not write anything. All I need is a property that returns my object.

+6
source share
3 answers

Update:

You are not using AAA syntax correctly. You no longer need an instance for the MockRepository (this was used for Rhino prior to 3.5). Just call the static methods in the MockRepository:

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

Here are some documents:

Original answer

Not. You no longer need to call Replay in situations like yours.

In previous versions, there was a record-repeat paradigm where you recorded expectations and played them during the test. It has been replaced with AAA syntax, where you can configure mocks much easier and more flexibly.

Behind the scenes, the recording and playback status of the layout is still preserved. Methods such as Stub put the layout in the recording state, configure it, and return to the recording. You do not need to explicitly call Record in these cases.

If you want to do a few more complicated operations, you can configure moker to reproduce the state yourself, do something with it, for example. to wait for reset:

 mock.BackToRecord(BackToRecordOptions.All); mock.Replay(); 
+6
source

Before the Replay method is called, RhinoMocks newbies are in a write state. This means that you can control how the layout behaves, even if you don't write anything by yourself, you still tell the layout how to behave isomegically, like Stub . Calling Replay stops your test from changing layout behavior and starts behaving as you indicated.

UPDATE

The Record method exists only to allow tests to move the mock object back to the recording state and change the layout behavior. I would highly recommend against this. I usually just use the methods MockRepository.ReplayAll() and MockRepository.VerifyAll() .

+1
source

Answer evacuated from question:

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

The last line puts the repository in the write state, however it is a simple stub. Therefore, a Replay necessary. For the AAA template, use different syntax:

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

Source: https://habr.com/ru/post/909263/


All Articles