• Blog
  • Archive
  • About
  • Contact
Sign in

Welcome to rickardnilsson.net

rickardnilsson.net is a weblog and the online home of web developer and father of three, Rickard Nilsson... More

Rickard blogs about creating software solutions using ASP.NET and agile practices.

Top Posts

  • Applying stylesheets dynamically with jQuery
  • ASP.NET MVC 2 Framework and Unity 2.0 Dependency Injection Container
  • Code Kata Cast
  • Dependency injection in ASP.NET MVC with Unity IoC Container
  • Isolate your code from ASP.NET with Moles Isolation Framework

Sites I've visited recently

  • Ninetech - Affärsnytta med IT
  • JetBrains Team City
  • Vimeo

Categories

  • .NET
  • Agile
  • ASP.NET 2.0
  • ASP.NET 3.5
  • ASP.NET MVC
  • BlogEngine.NET
  • C# 2.0
  • C# 3.0
  • CSS
  • Design by Contract
  • Design Patterns
  • iPhone
  • JavaScript
  • Kata
  • Moles
  • TDD
  • Testing
  • Unit testing
  • Unity
  • User tip

Five most recent posts

  • Prime Factors Kata in C#
  • iPhone developer
  • ASP.NET MVC 2 Framework and Unity 2.0 Dependency Injection Container
  • Isolate your code from ASP.NET with Moles Isolation Framework
  • Moles Isolation Framework from Microsoft to be compared with TypeMock Isolator

Tag cloud

  • agile
  • ajax
  • asp.net
  • asp.net 3.5
  • asp.net mvc 2
  • bdd
  • blog
  • blogengine.net
  • c#
  • cocoa touch
  • code kata
  • correction
  • css
  • dbc
  • dependency injection
  • design by contract
  • dom
  • douglas crockford
  • fakes
  • foto
  • getweekofyear
  • gregoriancalendar
  • highlight
  • html
  • httpcontext
  • humble dialog box
  • inversion of control
  • ioc container
  • iphone
  • iphone os
  • iso 8601
  • isolation
  • isolation framework
  • javascript
  • jquery
  • jscript
  • julian bucknall
  • klarsynt
  • live template
  • metaweblog api
  • microsoft research
  • mocks
  • model-view-presenter
  • moles
  • mvp
  • ninetech
  • objective-c
  • patterns & practices
  • photo album
  • picasa
  • recent posts
  • refactor
  • refactoring
  • release
  • resharper
  • rhino mocks
  • roy osherove
  • stubs
  • syntax
  • syntax highlighter
  • tdd
  • tdd masterclass
  • test coverage
  • testing
  • typemock
  • types
  • unit test
  • unity
  • unity 2.0
  • update
  • web service
  • week
  • widget
  • word 2007
  • yahoo
  • yui

How to unit test code which depends on HttpContext.Current.Server

Wednesday, 11 November 2009 22:33 by Rickard

Much of the legacy ASP.NET code I’ve seen is littered with calls to methods on the HttpServerUtility class,

Server.MapPath(…)

is only one such method. This makes it really hard to test. We need to be able to fake the MapPath method to return exactly what we want without doing the actual file mapping on disk.

Why, if your suite has thousands of tests and many calls IO or datebases, the tests will run slowly, and the developers on the team won’t run them as often. Ultimately, you may loose your investment in automated testing because it isn’t providing the promised feedback.

  • First of all, if the code is in the code behind of an aspx-file we need to extract as much as possible into its own class, which can be newed up in a unit test.
  • Second of all, we need to extract all external dependencies of the class such that fakes can be injected.

If the code behind code calls Server.MapPath() it is actually calling the Server property on the Page base class which returns HttpContext.Current.Server. This is an instance of the HttpServerUtility class, which is sealed and thus pretty impossible to fake out*.

Solution

In the namespace System.Web.Abstractions, which is part of ASP.NET 3.5, lives an abstraction of the HttpServerUtility, called HttpServerUtilityBase. It has a concrete implementation named HttpServerUtilityWrapper that takes an HttpServerUtility instance as a constructor parameter, as follows:

public sealed class HttpServerUtility {
    // ...
}

public abstract class HttpServerUtilityBase {
    // ...
}

public class HttpServerUtilityWrapper : System.Web.HttpServerUtilityBase {
    public HttpServerUtilityWrapper(HttpServerUtility httpServerUtility) {} 
    // ...
}

By leveraging a simple form of dependency injection we can preserve the old code as a first step of refactoring, and using an overloaded constructor to inject the fake object in our unit test.

public class Presenter {
    private HttpServerUtilityBase Server;

    public Presenter(HttpServerUtilityBase httpServerUtility) {
        Server = httpServerUtility;
    }

    public Presenter() {
        Server = new HttpServerUtilityWrapper(HttpContext.Current.Server);
    }

    public void PageLoad() {
        var path = Server.MapPath(…)
    }
}

Now, in a unit test for the Presenter class we can inject a fake server utility, which won’t call any IO.

[Test]
public void PageLoad_WhenCalled_ExpectedBehavior() {
    var fakeServerUtility = new HttpServerUtilityFake();  // implemented in the test suite
    var presenter = new Presenter(fakeServerUtility);
    presenter.PageLoad();
    // Assert expected behavior
}

Instead of implementing your own fake you can easily use your preferred isolation (mocking) framework of choice.

Conclusion

The goal is to isolate the class under test from all of its dependencies, weather they call IO, a database, a third party component, or even statics or touch static state. The point is that we want to assert that the class under test behaves as expected, not how the underlying framework behaves.

By leveraging the System.Web.Abstractions namespace we can preserve much of the existing ASP.NET code while covering it with tests.

_________
* Unless using TypeMock Isolator

Tags:   asp.net 3.5, unit test, agile, fakes, httpcontext, dependency injection
Categories:   Agile | ASP.NET 3.5 | Unit testing
Actions:  
Share | |
 
Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.
© 2008-2010 rickardnilsson.net
Creative Commons-licens