Hi,
When you mock Navigator, you are telling TypeMock to mock the next created instance of Navigator.
As this instance is never created (there is no new Navigator())you will fail,
What you have to do is create a
MockObject of
Navigator and then set the
PageControl.Navigator property to the Mocked Object.
Here is how:
MockObject mockNavigator = MockManager.MockObject(typeof(Navigator));
// set our mock object to be our navigator
PageControl aPageControl = new PageControl();
aPageControl.Navigator = (Navigator) mockNavigator.Object;
The same rational applies to the
Controls property. We will need to give it a fake implementation. Suppose we want an empty list.
In this case we cannot set the
Controls (it is private) so we will mock the
Controls property to return our fake list.
Here is how:
// mock the PageControl, because PageControl is a concrete type
// all methods will run as normal unless we say otherwise
MockObject mockPageControl = MockManager.MockObject(typeof(PageControl));
// our fake controls
List<System.Web.UI.Control> fakeControls = new List<System.Web.UI.Control>();
// inject it into our system
mockPageControl.ExpectGetAlways("Controls", fakeControls);
So our whole test looks like this:
[Test]
public void TestConstruction()
{
// check that we register the event
MockObject mockNavigator = MockManager.MockObject(typeof(Navigator));
mockNavigator.ExpectAddEvent("OnPageReloadHandler");
// create a PageControl we are going to stub the Control property
MockObject mockPageControl = MockManager.MockObject(typeof(PageControl));
PageControl aPageControl = (PageControl)mockPageControl.Object;
// inject our mock navigator
aPageControl.Navigator = (Navigator) mockNavigator.Object;
// our fake controls
List<System.Web.UI.Control> fakeControls = new List<System.Web.UI.Control>();
// inject them into our system
mockPageControl.ExpectGetAlways("Controls", fakeControls);
PlaceHolder aPlaceHolder = new PlaceHolder();
ConcretePage dummyPage = new ConcretePage(aPlaceHolder, aPageControl);
MockManager.Verify();
}
:idea: Now we can fire the event to see if everything works.
Change the second line to:
MockedEvent handler = mockNavigator.ExpectAddEvent("OnPageReloadHandler");
and you can fire the event by using
handler.Fire("message");
You will see your exception in OnPageReloadHandler begin throw!! Super Cool 8)