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:-
This example is a simple green field site; the typical File >> New Project >> MVC application.
So we have our site, I want data from a database (in this example I won’t be bothering with creating a data store, which shows just how handy stubs can be).
I want to run a test on the data and send it off to the view, simple stuff.
In Models, I want to create a data repository, but I don’t want to code against a concrete type, I will use an interface.
Here I have a simple method which in this case returns a hard coded integer.
So far so good, the site compiles.
I want the controller to handle this method call via an Order class like this.
As can be seen there are two SOLID principles in use here. The Order is dependent on the Repository class, but this dependency is injected in to the constructor. Also it is handed an interface and not a concrete type.
So my controller can call the GetTotalOrders method on the Order and let that handle the repository, but before all that I want a test in place that will make sure I am returning that data from the repository.
Of course in the real world I don’t want to touch the database, and in this case I don’t even have one. Here is the unit test that stubs out the repository call.
In this case I am forcing the repository to return an integer of 56, not the 55 from the concrete class. The solution compiles and the test passes.
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.