ASP.NET unit tests and the Page Lifecycle

I am writing unit tests for some ASP.NET webcontrols and have hit a brickwall. Some of these controls depend on the page lifecycle. For instance, one of the methods on a control works only on Prerender or after Page load. I tried the private object's invoke method like below, after adding my control to the page, and before calling the specific method of the control:
PrivateObject po = new PrivateObject(page);
po.Invoke("Page_Load", page, EventArgs.Empty);
However this doesn't solve my problem.

How do I go through the page lifecycle within my unit test?

Thanks.

[596 byte] By [tsg] at [2007-12-17]
# 1
ASP.NET tests always execute before Page_Load is called, and there's unfortunately no way to let the lifecycle continue naturally from within a test. However, you can call Page_Load, etc, through the private object, as you've done. What is the specific scenario that isn't working for you--what changes based on lifecycle? I verified that calling Page_Load works in a very simple case, so I'd like to know more about what your case is.
KevinCoggerMSFT at 2007-9-9 > top of Msdn Tech,Visual Studio Team System,Visual Studio Team System - Web and Load Testing...
# 2

Kevin,

I am writing tests for custom web controls. I am doing this by dynamically adding the custom controls to the page and calling the controls' properties and methods from within an ASP.NET unit test in Team Test. I invoked the Page_Load as I mentioned earlier but this call does not in turn call the protected OnLoad methods of the added child controls on the page. Is this a known issue, or an expected behavior? What is the recommended way for testing web controls using team test?

Thanks

tsg at 2007-9-9 > top of Msdn Tech,Visual Studio Team System,Visual Studio Team System - Web and Load Testing...
# 3

Sorry for the late reply. This is essentially expected behavior, as Page_Load itself doesn't automatically call the OnLoad methods--they're normally called as part of the page lifecycle after Page_Load has executed. What you can do in a test is manually call the OnLoad methods through the PrivateObject, the same way you called the Page_Load method. This is definitely a limitation that makes situations like yours more difficult than they could be, since there's no way to make the page lifecycle progress normally. I'm going to file a suggestion for the next version about this because it looks like it would definitely be helpful.

One other thing you may consider is that if these controls aren't actually dependent on ASP.NET state, it may be possible to test them outside the ASP.NET context, using regular unit tests. However, I don't know if this will be useful for your case or not.

KevinCoggerMSFT at 2007-9-9 > top of Msdn Tech,Visual Studio Team System,Visual Studio Team System - Web and Load Testing...
# 4

Hi,

Just checking to see if there is any update on this issue? Please let me know.

Thanks.

tsg at 2007-9-9 > top of Msdn Tech,Visual Studio Team System,Visual Studio Team System - Web and Load Testing...

Visual Studio Team System

Site Classified