This project is read-only.

Mocking the View

TODO: write about...
  • using MoQ to mock a View in a unit test that tests the Presenter

// Create the mock
Mock<IViewPortalUsersView> mockedView = new Mock<IViewPortalUsersView>();

// Setup all properties

// send the mock into the Presenter
ViewPortalUsersPresenter presenter;
presenter = new ViewPortalUsersPresenter(mockedView.Object, mockedController.Object) {
                       HttpContext = CreateMockedHttpContext().Object };

// raise an event
mockedView.Raise(self => self.Initialize += null, new System.EventArgs());

// Verify some action on the mocked view
mockedView.Verify(self => self.ShowAddButton(false)); // check that a method has been called with a specific argument
mockedView.Verify(self => self.SetMinMax(It.IsAny<int>(), It.IsAny<int>())); // check that a method was called with any value
mockedView.Verify(self => self.HighlightUserId(It.IsAny<int>()), Times.Never()); // check that a method has NOT been called

// Assert something about the View that should have been set by the Presenter
Assert.AreEqual(expectedModuleId, mockedView.Object.Model.ModuleId);

Last edited Jul 3, 2010 at 5:28 AM by dagilleland, version 1


No comments yet.