Friday, May 2, 2008

Testing ASP.NET Event callback methods using VSTS

Unit Testing a ASP.NET Application is a complex activity because the execution of a ASP.NET page also depends on the lot of information from the "context". A test method of a code behind must contain the following additional attributes.

1. UrlToTest - As the name suggests, it will be the complete URL to be tested.
2. HostType - Value of this will be ASP.NET
3. AspNetDevelopmentServerHost - This attribute has 2 arguments.First the physical directory of the web app and second the application root name.

Once you do this, using the TextContext Object you can now access the Page and all the controls (using page.FindControl method) within the page with the help of RequestedPage property.

To invoke the eventhandler methods which usually are not public, one can use the following approach

PrivateObject priobj = new PrivateObject(TestContext.RequestedPage);
priobj.Invoke(eventhandler_method, args1, args2);

No comments: