Sunday, November 16, 2008

Convert to Arrange, Action, Assert

I am studying RhinoMocks, the new Arrange/Action/Assert style is more concise than the Record/Replay. Compare the style below

public void Analyze_WebServiceThrows_SendsEmail() { MockRepository mocks = new MockRepository(); IWebService stubService = mocks.Stub<IWebService>(); IEmailService mockEmail = mocks.CreateMock<IEmailService>(); using (mocks.Record()) { stubService.LogError("whatever"); LastCall.Constraints(Is.Anything()); LastCall.Throw(new Exception("fake exception")); mockEmail.SendEmail("a", "subject", "fake exception"); } LogAnalyzer2 log = new LogAnalyzer2(); log.Service = stubService; log.Email = mockEmail; string tooShortFileName = "abc.ext"; log.Analyze(tooShortFileName); mocks.VerifyAll(); } [Test] public void Analyze_WebServiceThrows_SendsEmail_aaa() { //arrange var webServiceStub = MockRepository.GenerateStub<IWebService>(); var emailMock = MockRepository.GenerateStub<IEmailService>(); webServiceStub.Stub ( x => x.LogError("whatever")) .Constraints(Is.Anything()) .Throw(new Exception("fake exception") ); //act new LogAnalyzer2() { Email = emailMock, Service = webServiceStub }.Analyze("abc.ext"); //assert emailMock.AssertWasCalled(x => x.SendEmail("a", "subject", "fake exception")); }

No comments:

Post a Comment