Code Monkey home page Code Monkey logo

aspnetmvcmockingsessionstate's Introduction

ASP.NET MVC: Mocking Session State

Introduction

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 3.0.81.0.

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!

[TestMethod]
public void About()
{
    TestControllerBuilder builder = new TestControllerBuilder();

    // Arrange
    HomeController controller = new HomeController();

    builder.InitializeController(controller);

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

    // Assert
    Assert.IsNotNull(result);
}

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.

aspnetmvcmockingsessionstate's People

Contributors

geersch avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.