andrewlocatelliwoodcock

Thoughts on Software

Unit Testing Session State Dependent Code in ASP.Net MVC

with 2 comments


It can be pretty tricky to unit test code that’s dependent on Session State, a situation I needed to address recently when developing my talk for the MTUG Developer Day, largely because Session State is dependent on having a properly initialized Controller and a Controller, as it is built on top of ASP.Net is not an easy thing to initialize!

The MVCContrib Testhelper can help here though as it has a handy method to … initialize a Controller!

Here’s a sample:

var controller = new XXXController();

            // MVCContrib TestHelper is initializing the Controller for us INCLUDING session state!
            var builder = new TestControllerBuilder();
            builder.InitializeController(controller);

The InitializeController method performs all the necessary tasks to initialize the Controller including preparing Session State and we can now unit test code that depends on Session State.

Written by andrewlocatelliwoodcock

October 25, 2011 at 20:26

2 Responses

Subscribe to comments with RSS.

  1. … [Trackback]…

    […] Read More here: andrewlocatelliwoodcock.com/2011/10/25/unit-testing-session-state-dependent-code-in-asp-net-mvc/ […]…

    E-bike

    November 13, 2011 at 12:53

  2. I like this internet site its a master peace ! Glad I identified this on google .

    giant twist comfort cs

    November 13, 2011 at 18:33


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: