Over a million developers have joined DZone.

ASP.NET MVC: How to Mock Session State


IntroductionASP.NET MVC Contrib

A couple of months ago I wrote a post on unit testing using NUnit & Moq which showed how you can use the Moq framework to remove database dependencies from your unit tests.

Last week I got a question about how to mock the session state when unit testing your controller actions. This can be quickly achieved using the MVC Contrib library provided by the Outercurve Foundation.

It contains a TestHelper library which helps you to quickly create unit tests and reduce your workload. Let’s see how we can use it to our advantage…

Hello, World!

First we need to create a demo ASP.NET MVC application for which we can write unit tests. Start up Visual Studio 2010 and create a new blank solution called “SessionState”. Next add a new ASP.NET MVC 3 Web Application to the solution originally called “MvcApplication”. Be sure to choose the Internet Application template and check the “Create a unit test project” option.

ASP.NET MVC 3 Application

Once you click OK Visual Studio will add a new ASP.NET MVC 3 project and a unit test project to your solution. The unit test project already contains two unit tests for the web application’s HomeController. Just open the HomeControllerTest.cs file. You can run them by clicking Test > Run > All Tests in Solution.

Running Unit Tests

Test Results

Everything is well. The tests pass. Now let’s break them!


Breaking the Unit Tests

Let’s quickly break a unit test. Open the HomeController code file and adjust the About() action method so that it resembles the following listing.

public ActionResult About()
    Session["FooBar"] = "BOOM";

    return View();

If you run the web application you won’t notice a difference, but if you run the unit tests you’ll notice that one of them blows up because the Session object is not set to an instance of an object (it is NULL).

Test Failed


MVC Contrib

Let’s fix the broken unit test by introducing the TestHelper library offered by the MVC Contrib project. I downloaded this library via NuGet by adding a library package reference to the unit tests project (MvcApplication.Tests). The id of the package is “MvcContrib.Mvc3.TestHelper-ci”. At the time of writing it is at version

NuGet Library Package Reference

If you are not familiar with NuGet yet, be sure to read up on it. You can also download the library (MVCContrib.Extras.release.zip) from CodePlex. Make sure you download the MVC 3 version.

Now we need to fix the HomeController unit tests. Open up the HomeControllerTests.cs code file and add “MvcContrib.TestHelper” to the using statements. Next we need to adjust the unit test for the About() action method. By simply adding two lines (TestControllerBuilder) we are able to mock the session state!

public void About()
    TestControllerBuilder builder = new TestControllerBuilder();

    // Arrange
    HomeController controller = new HomeController();


    // Act
    ViewResult result = controller.About() as ViewResult;

    // Assert


The TestController’s InitializeController(…) method takes our controller (HomeController) and initializes its internal data members (HttpContext, HttpSession, TempData…) correctly. If you re-run the tests they’ll pass.

Tests Re-run

If you debug the tests and put a breakpoint after the controller has been initialized you’ll notice that the session object has been mocked.

Mocked Session State

That’s all there is too it. Just add a reference to the MvcContrib.TestHelper library, create a TestControllerBuilder instance and initialize the controller. Voila, you can now create unit tests for controller action methods which depend on the session state.

You can find the source code for this article on the download page of this blog. If you have any questions or suggestions please feel free to leave a comment or contact me.


Published at DZone with permission of Christophe Geers, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}