Pages

Friday, June 21, 2013

Beginning ASP.NET MVC dependency injection and automated testing

Let's take a look at testing the simplest MVC controller that could prove the worth of automated testing/dependency injection.

Our task is the following:
  •  For users configured to view English webpages, output "Hello World!" 
  •  For Users configured to view Spanish webpages, output "Hola Mundo!"
  • For users configured to view any other language, throw an exception.
We will prove our application performs this way via automated tests.

The way we know a user's language preference is via the 'Accept-language' header in an HTTP request.


Getting this information out of the request is simple.  Anywhere in your code you can write the following to get a user's most preferred language :


BUT you have to be sure that HttpContext has its properties set (its properties are set in thread local storage by ASP.NET, for those wondering).  In other words, this will only work when executing as part of a web request.

This poses a problem for unit testing.  When you write a test like this:

It will throw an exception when it tries executing your controller's action because the test runner isn't running your code with a set HttpContext.

Dependency Injection

The best way to make our class testable is to use dependency injection.  

We're going to setup our controller to get the preferred language from a class that implements IRequest, an interface that looks like this


And we'll implement it using the one liner for preferred language like we did above 


The major difference to the code we are trying to to test will be that we going to have our controller rely on the IRequest interface as opposed to using the HttpContext class directly. Our class will look like this :


Now, all we need to test our controller is a stub of IRequest that will allow us to specify the incoming language.


This now allows us to write three comprehensive tests where we can actually specify the language ourselves and prove the behavior of our controllers even though we aren't actually receiving an HTTP request.

In production : [Inject]

For this to work in production, you may have noticed in the HomeController class I'm using an 'Inject' attribute which will eventually resolve to an instance of  the 'Request' class when the code is serving a real HTTP request.

How does this work?

I'm using a third party library called Ninject (the MVC 3 package) that lets me easily specify these bindings for the production run of the app.  Meaning, that after HomeController gets allocated, the IRequest will automatically be set to my Request concrete class.

Obviously this mapping must be done to ensure that when my code is running as an MVC application, and not in unit tests, that its using the class that utilizes the real HttpContext.

Conclusion

What we have accomplished

  • Wrote a controller that only depends on an interface, not a built-in ASP.NET class that only works inside an HTTP request.
  • Proved our controller works for three different language scenarios (would be quite a pain to have to change your browsers language setting to test each scenario).
  • Can see that dependency injection is useful for unit testing classes that have external dependencies.  Other external dependencies include : file systems, databases, and web services. 
  • Used Ninject to make property injection as painless as possible in production.


No comments:

Post a Comment