Question

I created a class that inherits System.Web.UI.Page.

In that class I override some application lifecycle events like page_load and page_init. Before calling the base of those events I check session values, cookies and things like that.

I would like to Unit test the methods that check the sessions and cookies. I am already able to mock the session and cookie objects. Is it possible to Unit test the application life cycle events? Or is my only option to totally re-factor the class so that all methods called from within the life cycle events are in a separate class?

Was it helpful?

Solution

If you are able to mock the pertinent objects, you could simply call the lifecycle methods directly. But I don't know if that's a good idea; IMHO, any single lifecycle event handler should be doing multiple tasks, each of which should probably be delegated to some other class - and it is that code you should be unit testing.

My personal habit is that I test utility/service/repository code directly, and leave code in the lifecycle events directly only that would apply under integration tests, rather than unit tests.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top