chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
Welcome to Typemock Community! Here you can ask and receive answers from other community members. If you liked or disliked an answer or thread: react with an up- or downvote.
0 votes
I'm struggling to figure out how to mock and test code in a parent Page class. The parent Page class has the following code:

protected virtual void Page_Load(object sender, System.EventArgs e)
{
  this.Overrides();
}

protected virtual void Overrides()
{
  // --- Some initialization code...
}


I want to be able to fire the Page_Load event and then verify expectations occurred within the Overrides() method. From what I've read so far, I can't use Natural Mocks because the event is protected. I've tried various approaches but cannot seem to get it to work.

Can anyone give me a hand?
asked by kyleheon (3.1k points)

7 Answers

0 votes
Hi,

Take a look at this page from the Isolator Guide, which describes events using reflective mocks (which allow access to non public members):

https://www.typemock.com/Docs/UserGuide/ ... ctive.html
answered by gilz (14.5k points)
0 votes
Also check out Ivonna for ASP.NET testing:
http://www.sm-art.biz/Ivonna.aspx
answered by royo (2k points)
0 votes
How exactly would I go about using Ivonna for this? At the moment I'm not even working with a Web project, let alone a web page. This is all functionality bubbled up into a shared class library.

Every page in our system will derive from this same PageTemplate class.
answered by kyleheon (3.1k points)
0 votes
Hi,

Ivonna is great for ASP.Net projects. If this is indeed a shared library, you might be better off with the Isolator methods. Did using reflective mocks for events do the trick?
answered by gilz (14.5k points)
0 votes
I've tried a number of times but can't figure out how I go about mocking Page_Load when the event isn't wired up within my base page class but instead handled by the ASP.NET process.

I want to force the event to fire so that I can test the post event results. I don't necessarily care to mock that an event was registered.

Any suggestions?
answered by kyleheon (3.1k points)
0 votes
You have two choices here: to fire an event or to call the Page_Load method manually.

1. Set AutoEventWireup to false and add a handler manually in the constructor. This way it's wired up in your own code, and you can test it as it was suggested.
2. Call the method manually, it's a more "unit" way of testing (you don't test the wiring of the event, just the method). You'll have to either set it to be public (why not?), or invoke it via reflection.

ulu
answered by ulu (1.7k points)
0 votes
Hi Kyle,

I'll choose ulu's second option (reflection) to invoke the method.
Here's the ParentPage class:
    public class ParentPage: Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Overrides();
        }

        protected virtual void Overrides()
        {
            InnerClass.DoSomething();
        }
    }
    
    internal class InnerClass
    {
      internal static void DoSomething()
      {
          Console.WriteLine("DoSomething Was called");
      }
    }


Now, let's say I want to mock the DoSomething method on the inner class, as well as verify that it has been called. I can do something like this:
        [TestMethod]
        [VerifyMocks]
        public void MockDoSomething_VerifyItWasCalled()
        {
            Mock pageMock = MockManager.Mock<InnerClass>();
            pageMock.ExpectCall("DoSomething");

            ParentPage page = new ParentPage();
            MethodInfo method =  page.GetType().GetMethod("Page_Load", BindingFlags.Instance | BindingFlags.NonPublic);
            method.Invoke(page, new object[]{this, null});
        }


Note the binding flags I use to invoke the instance's internal method.

I hope this example helps. Let me know if you need additional help.
answered by gilz (14.5k points)
...