I've migrated my blog

Thanks for visiting my blog. I am no longer maintaining this and I've migrated the blog to my personal domain . If you would like to follow my blog kindly use my new RSS feed

Saturday, February 11, 2012

Unit Testing Custom Model Binders in MVC3

 In my previous post we have seen a way to do unit testing with Sessions in MVC3 using Custom model binders. In this blog post we are going to see how to do unit test the model binder itself.
One remarkable thing which everybody hails in MVC3 is its extensibility and its testability. You can extend/customize the components in the framework and also you can unit test them with ease.
Before getting into unit testing the custom model binder, Lets have a closer look at the BindModel method

image

In the BindModel method we are making use of Session property in the HttpContext object which is in turn a property of the ControllerContext object that is passed to the BindModel method as a parameter by the MVC3 framework. In order to unit test this method we need to have to control over the HttpContext property of the ControllerContext and the Session property of the HttpContext.

How to get control over those properties ? Thanks to a constructor of ControllerContext

image

ControllerContext uses the constructor dependency injection to get rid of the direct dependency on HttpContextBase and we are going to exploit this to do unit testing. Using a mocking framework we can easily create a mock of HttpContetBase and drive the unit test.

The Session property of the HttpContextBase is of type HttpSessionStateBase which can also be mocked.
Here is the complete implementation of Test fixture class which unit test the CartModelBinder class that we have seen in the previous post using the mocking library Moq.

CartModelBinderTests

I hope the test code is self-explanatory.

Thursday, February 2, 2012

Unit Testing with Sessions in ASP.NET MVC3

Introduction


While talking to my friend regarding his project, he told me about how he is doing unit testing which involves sessions in ASP.NET MVC3. His team is actually using a “HttpSimulator” which simulates the web request and then the do unit test by verifing the session by interacting with the simulator. When digging further I have come to know that this way of unit testing session objects are influenced from the “ASP.NET Webforms”. It reminds me the talk of Neil Ford on Function Programming. In that video he talks about an analogy called “Axe and Chain Saw” to explain our way of thinking as

When we give a chain saw to people who were cutting trees by axe, they would tend to use chain saw in the same way as the use Axe. Which is obviously inefficient. So we should understand at the capabilities of the tool in our hand before we using it”. 

ASP.NET MVC3 is far better than Web Forms when it comes to unit testing. We don’t need to use a simulator to test against our sessions. There is a better way to do this MVC3 and in this blog post we are going to explore it.

Time for Code

Shopping Cart is the first thing that strikes our mind when we want to quote an example for using Http session. So, I am going to show an app called “MyShop” a mini shopping site through which I am going to explain the concepts involved. The application flow would be as follows


image
image


Models

The models are simple, straight forward and self explanatory.
image
image

I’ve tried my level best to keep the model as simple as possible. So, Cart in MyShop will have only two public methods. One to add a product to the Cart’s Line and another one to retrieve all the products inside the Cart’s line.


The CartController Version 1.0

image

In this CartController version 1.0 we have two public methods Index and AddToCart which are dependent on HttpSession object. This dependency inside the methods is actually preventing us from unit testing the CartController in simple way and we have no choice other than implementing a “Http Simulator” to unit test these two methods. As I said before there is better to do is! Here we go!!


The CartController Version 2.0

image

No more Sessions!!.. Yeah.. We have got rid of the dependency on the session object by adding a new parameter called cart. Now you can use easily unit test the CartController as follows

image

Okay we made it easy for unit testing by moving the dependency out of the method and introduced the cart as the parameter. But how does my MVC3 framework will know the cart parameter should come from session object ?… Good Catch!! and here comes the magic called custom ModelBinder


ModelBinder – A brief background

Model binding is an exciting feature in MVC3 framework which automatically creates the C# objects directly from Http request and pass it to the Action methods in controller as parameter values. It uses a default model binder which looks at the form values, query string values that are submitted with the Http Request and create the model object.


CartModelBinder

In our case, we need to have a object of Cart which is populated from the Session object and not from the HttpRequest. The default model binder used by MVC3 has no idea about session object. So, Its our responsibility to tell to the MVC3 framework

Hey! If there is any parameter of type Cart in controller action method, then use my own custom model binder called CartModelBinder to create the object

There are two steps to do the above said operation

1. Creating the custom model binder by inheriting the IModelBinder interface

image

2. Registering our custom model binder in the Global.asax.cs file

image

That’s all.. MVC3 takes care of rest


Summary

In this blog post we have explored how we can get rid of “Http Simulator” to unit test the controllers which involves Session objects using custom model binder. You can download the working example of “MyShop” showcased in this blog post from here. Refer my next blog post to check out how to do unit testing with the custom model binder itself.