views:

99

answers:

2

As I understand it, a mock object created with RhinoMocks enter the recording state directly when it is created, and then you call Replay() to enter the replay state. I would like to manually decide when the mock object starts recording, or be able to pause the recording. Would that be possible in RhinoMocks?

Thanks /Erik

A: 

In my opinion it's better to use the Arrange Act Assert format.

var mockEmailService = MockRepository.GenerateMock<IEmailService>();
mockEmailService.Expect(x => x.Send("me@home", "Subject", "Body"));

//Thing to test
var controller = MehController(mockEmailService);
controller.Meh();

mockEmailService.VerifyAllExpectations();

If you need to use the object before it goes into playback mode then there is something wrong with your test.

Peter Morris
+3  A: 

Mocks are either in record or replay mode. They can't be in a "nothing" mode.

If you don't want to use the AAA syntax and you want to control the record/replay state then you'll have to do it manually by calling the mockRepository.Replay(mock) method immediately after creating the mock.

Use the mockRepository.BackToRecord(mock,option) method to put the mock back in record mode. Use the BackToRecordOptions.None option to prevent the clearing of any expectations you have already set.

Richard Banks