问题
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
回答1:
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.
回答2:
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.
来源:https://stackoverflow.com/questions/519666/is-there-a-way-to-decide-when-a-rhinomocks-mock-starts-recording