A Test-Driven Dev's Nightmare: Meeting the HttpContext
2 min read
2 min read
My main task for the coming weeks at work is to extend one of our older but highly successful web application with new functionalities. The webapp has become quite complex over the years, as it happens with most applications when they're being extended. Unfortunately, devs have not written any automated tests. Not having any tests in place that could function as regression suite is quite bad, as any change turns out to be risky because you don't have any certainty on whether you didn't destroy any previously implemented functionalities.
So what I started with was to create a unit-test project and to write some tests that did at least cover the areas where I was going to modify code. My good intentions didn't last long...one of my tests immediately failed, returningObject reference not set to an instance of an object"I probably need to mock something out", I thought...navigated to the code where the exception occurred and ... gave up. A static helper class used throughout the whole business logic accessing the ASP.net HttpContext...
public static class SomeHelper...a nightmare.
{
...
public static bool IsPowerUser
{
get
{
return HttpContext.Current.User.IsInRole(AccountRoles.Office.ToString())
&& HttpContext.Current.User.IsInRole(AccountRoles.PowerUser.ToString());
}
}
...
}