Unit Testing ASP.NET User Controls
page 3 of 5
by Brian Mains
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 30316/ 49

Initial Assignments

ASP.NET events in the page lifecycle can be manually triggered. When testing a single ASP.NET user control, each of the event methods can be called to simulate an ASP.NET user control's lifecycle. For instance, to call these methods the following initialization sets up the page lifecycle.  All that is needed is a call to the associated On<Event> method because these methods eventually call the appropriate event, as defined in the Control class. That is one of the beauties of using inheritance in this situation; it allows you to have direct access to these methods. Note that reflection could be used in a non-inheritance scenario.

Listing 2

[TestFixtureSetUp]
Public void Initialize()
{
  this.Page = new AssignmentPageTest();
  this.OnInit(EventArgs.Empty);
  this.OnLoad(EventArgs.Empty);
  this.OnPreRender(EventArgs.Empty);
}

In ASP.NET the Page property is assigned upon creating the user control in the page lifecycle. In a unit-testing environment, this assignment is not automatic, so a class that inherits from System.Web.UI.Page is passed to the property. But the question is, why the AssignmentPageTest class?

Each user control is usually a specialized section of a target page. This approach is usually more coupled than general ASP.NET user controls individually because the custom user control class is directly coupled with the ASCX user control. That is not necessarily true in all situations though, but is true more often than not.

Because this control would normally be a part of the AssignmentPage custom page class, it seems most fitting to use that. However, AssignmentPage most likely is a defined abstract, while AssignmentPageTest is a concrete implementation of the class that the unit test knows about.

Therefore, an instance of the concrete implementation of AssignmentPage is created and passed to the Page property. This ensures the user control can use Page properties, like IsPostback, within the code without an exception being thrown (because the Page property would be null otherwise).

When used in the context of a custom page unit test (such as AssignmentPageTest), the page must invoke its methods. Then, each subsequent user control must invoke its own methods as well; these are not automatically called for you. Though not directly accessible in the page unit test setting, reflection can be used to invoke the OnInit, OnLoad, and OnPreRender methods (as well as any other relevant methods you use).


View Entire Article

User Comments

Title: mr   
Name: rob
Date: 2012-03-15 4:55:09 AM
Comment:
would love a complete solution with usercontrol project and test projects for reference.

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-03-28 6:11:16 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search