Is there a way to decide when a RhinoMocks mock starts recording?

时光总嘲笑我的痴心妄想 提交于 2019-12-24 00:26:02

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!