Web EricSavage  -  19 Jun 2009 11:18 am

Using a fake session for testing

I’ve been working on an application that uses Habanero and Visual WebGui (VWG) to provide a simple website that lets a department manage a list of facilities. At Chillisoft we use test-driven development to build robust applications, and we attempt to get our business, logic and presentation layer as close to 100% test coverage as possible.

I was writing tests for a section of code that calls through to VWGContext.Current.Session, which effectively allows you to store and retrieve session variables in the same way you do in ASP.Net. The trouble is that Current returns null because no web environment has been launched.

I developed a workaround class that allows you to switch between using the real session and fake internal session. The end behaviour is exactly the same, but you’re obviously able to get your test layer running correctly.

The code is listed below and you can adapt it for ASP.Net quite easily by just replacing VWGContext with HttpContext. I do have a nUnit test class for this as well - just enquire below if you would like it.

using System.Collections.Generic;
using Gizmox.WebGUI.Forms;
 
namespace MyProject
{
    /// <summary>
    /// Provides a way to fake out your sessions for the purpose of testing.
    /// Defaults to using the VWG Session, but you can set UseRealSession to
    /// false to manage a fake session that stores values in the same way a
    /// web session does.
    /// </summary>
    public class SessionManager
    {
        private static SessionManager _current;
        public bool UseRealSession { get; set; }
        private Dictionary<string, object> _fakeSession =
            new Dictionary<string, object>();
 
        /// <summary>
        /// Gets and sets a particular session variable.  Returns null if you
        /// access a variable that has not been stored.
        /// </summary>
        public object this[string propertyName]
        {
            get
            {
                if (UseRealSession)
                {
                    return VWGContext.Current.Session[propertyName];
                }
 
                if (!_fakeSession.ContainsKey(propertyName)) return null;
                return _fakeSession[propertyName];
            }
            set
            {
                if (UseRealSession)
                {
                    VWGContext.Current.Session[propertyName] = value;
                    return;
                }
 
                if (_fakeSession.ContainsKey(propertyName))
                {
                    _fakeSession[propertyName] = value;
                }
                else
                {
                    _fakeSession.Add(propertyName, value);
                }
            }
        }
 
        /// <summary>
        /// Clears the values stored in the "false" session.
        /// This is useful for testing, where every test needs
        /// to start from a clean slate.
        /// </summary>
        public void Clear()
        {
            _fakeSession = new Dictionary<string, object>();
        }
 
        /// <summary>
        /// Gets the current session or creates a new one if not available.
        /// The session uses the real VWG session by default - call
        /// <code>SessionManager.Current.UseRealSession = false</code>
        /// if you want to use the fake session manager.
        /// </summary>
        public static SessionManager Current
        {
            get
            {
                if (_current == null)
                {
                    _current = new SessionManager();
                    _current.UseRealSession = true;
                }
                return _current;
            }
            set
            {
                _current = value;
            }
        }
    }
}

You can use the code in the following way:

SessionManager.Current["name"] = "Eric";
string name = (string)SessionManager.Current["name"];

General use of this code in a working application requires no setup, since the SessionManager defaults to using VWGContext. However, when you are writing a test class that tests code using the sessions, you’ll need to modify the TestFixtureSetUp and the SetUp:

[TestFixtureSetUp]
public void SetupTestFixture()
{
    SessionManager.Current.UseRealSession = false;
}
 
[SetUp]
public void Setup()
{
    SessionManager.Current.Clear();
}

Any feedback is welcome.

One Response to “Using a fake session for testing”

  1. on 01 Jul 2009 at 7:20 am 1.Using a fake session for testing | XML Developer said …

    [...] Read the original: Using a fake session for testing [...]

Trackback This Post | Subscribe to the comments through RSS Feed

Leave a Reply