When I was first attracted to the Microsoft MVC Framework, one of my main ambitions was to develop using a more test driven approach. There are ways to include Unit testing with WebForms, but the friction was just too much to justify on the web projects I was involved with. As soon as I started using MVC I was amazed at how easy it was to incorporate unit testing and isolation frameworks; in a way it became more of a natural process. Here in this post I am quickly covering getting up and running with unit testing, isolation frameworks and MVC. My tools of choice are:-
Great, but how do we code this for the controller.This works fine, but I don’t want the Index method to be responsible of creating the Repository object, so we take that out and let the constructor handle that.Now it compiles ok, the tests run ok, but the web application fails.
This is where we need to start injecting the dependencies needed. We need both an ApplicationRegistry and Bootstrapper class.The ApplicationRegistry class reads very fluently and simply injects any occurrence of the IRepository type with the Repository concrete type. Next we need a controller factory which inherits from the MVC DefaultControllerFactory and is called from the Application_Start in the Global.asax file.Now rebuilding and running the site we successfully retrieve the value from the repository.