A quick note on how to test for event assignments using NUnit and Rhino.Mocks for stub and mock scenarios.
Stub scenario:
- [Test]
- public static void AssignPageEventHandlers_SetsPageEventHandlers()
- {
- var stubView = MockRepository.GenerateStub<INavigationPageBaseView>();
- var obj = new TestNavigationControllerRoot(stubView);
- obj.AssignPageEventHandlers();
- stubView.AssertWasCalled(v => v.PreInit += Rhino.Mocks.Arg<EventHandler>.Matches(eh => eh.Method.Name.Equals("PagePreInit")));
- stubView.AssertWasCalled(v => v.Init += Rhino.Mocks.Arg<EventHandler>.Matches(eh => eh.Method.Name.Equals("PageInit")));
- stubView.AssertWasCalled(v => v.Load += Rhino.Mocks.Arg<EventHandler>.Matches(eh => eh.Method.Name.Equals("PageLoad")));
- }
Mock scenario:
- [Test]
- public void PageLoadMasterController_ExpectedBehavior()
- {
- var stubExProcessor = MockRepository.GenerateStub<IExceptionProcessor>();
- stubExProcessor.Stub(e => e.GetCurrentUserErrorMessage()).Return("msg");
- var mockView = Mocks.StrictMock<IErrorView>();
- using (Mocks.Ordered())
- {
- Expect.Call(() => mockView.PreInit += Arg<EventHandler>.Matches(eh => eh.Method.Name.Equals("PagePreInit")));
- Expect.Call(() => mockView.Init += Arg<EventHandler>.Matches(eh => eh.Method.Name.Equals("PageInit")));
- Expect.Call(() => mockView.Load += Arg<EventHandler>.Matches(eh => eh.Method.Name.Equals("PageLoad")));
- Expect.Call(mockView.IssueMessageLiteralText = "msg");
- }
- Mocks.ReplayAll();
- var obj = new ErrorController(mockView) { NavigationBuilderFactory = MockNavBuilderFactory, NavigationMasterController = MockNavigationMasterController };
- obj.PageLoad(stubExProcessor);
- Mocks.VerifyAll();
- }
No comments:
Post a Comment