mocking session variable in unit test using Moles
Asked Answered
H

1

3

Method I am unit testing checks for a session variable like

if(Session["somevar"] != null)
{
   // rest of the code
}

In my test, not able to get rid of this since Session is null, it's throwing null referrence exception.

To bypass this, I have tried mocking it like below but no luck

System.Web.Moles.MHttpContext.AllInstances.SessionGet = (HttpContext cntx) =>
{ return (HttpSessionState)cntx.Session["somevar"]; }

I even tried method mention here to simulate HttpContext and then doing below

HttpContext.Current = new HttpContext(workerRequest);
HttpContext.Current.Session["somevar"] = value;

But again no luck. This time, though the HttpContext.Current is not null but HttpContext.Current.Session and hence throws null ref exception.

Any idea how can I mock this/by pass this in my test [Without using any external DLL or main code change. Sorry, but can't afford to do so].

Thanks and appreaciate lot your help.

Hysterics answered 13/12, 2011 at 10:41 Comment(1)
possible duplicate of How do I mock/fake the session object in ASP.Net Web forms?Militate
M
4

Update 2013:

The bad news now is that the Moles framework was a Microsoft Research (MSR) project, and will not be supported in Visual Studio 2012. The great news is that Microsoft has now integrated the MSR project into the mainline framework as Microsoft Fakes.

I found an article that solves the problem you had, using the Fakes framework instead of the Moles framework:

http://blog.christopheargento.net/2013/02/02/testing-untestable-code-thanks-to-ms-fakes/

Here's an updated version of my previous answer that uses the Fakes framework instead of Moles.

using System.Web.Fakes;

// ...

var sessionState = new Dictionary<string, object>();

ShimHttpContext.CurrentGet = () => new ShimHttpContext();
ShimHttpContext.AllInstances.SessionGet = (o) => new ShimHttpSessionState
{
    ItemGetString = (key) =>
    {
        object result = null;
        sessionState.TryGetValue(key, out result);
        return result;
    }
};

You might even be able to make it look more like the Moles version I posted before, though I haven't tried that out yet. I'm just adapting the article's code to my answer :)


Before 2013 edit:

You said you want to avoid changing the code under test. While I think it should be changed, as directly accessing session state like that is a bad idea, I can understand where you're coming from (I was in test once...).

I found this thread describing how someone moled both HttpContext and HttpSessionState to get around this problem.

Their code ended up looking like this:

MHttpContext.CurrentGet = () => new MHttpContext
{
    SessionGet = () => new MHttpSessionState
    {
        ItemGetString = (key) =>
        {
            if (key == "some")
                return "someString"/* or any other object*/;
            else return null;
        }
    }
};

I'd go even farther and implement ItemGetString with a dictionary:

var sessionState = new Dictionary<string, object>();

MHttpContext.CurrentGet = // ...
{
    // ...
    ItemGetString = (key) =>
    {
        object result = null;
        sessionState.TryGetValue(key, out result);
        return result;
    }

Before edit:

I usually solve problems like this by encapsulating global state with an abstract class or interface that can be instanced and mocked out. Then instead of directly accessing the global state, I inject an instance of my abstract class or interface into the code that uses it.

This lets me mock out the global behavior, and makes it so my tests don't depend on or exercise that unrelated behavior.

Here's one way to do that (I'd play with the factoring a bit tho):

public interface ISessionContext
{
    object this[string propertyName] { get; set; }
}

public class ServerContext : ISessionContext
{
    public object this[string propertyName]
    {
        get { return HttpContext.Current.Session[propertyName]; }
        set { HttpContext.Current.Session[propertyName] = value; }
    }
}

public class SomeClassThatUsesSessionState
{
    private readonly ISessionContext sessionContext;

    public SomeClassThatUsesSessionState(ISessionContext sessionContext)
    {
        this.sessionContext = sessionContext;
    }

    public void SomeMethodThatUsesSessionState()
    {
        string somevar = (string)sessionContext["somevar"];
        // todo: do something with somevar
    }
}

This would require changes to your code-under-test, but it is the type of change that is good both for testability and for portability of the code.

Militate answered 13/12, 2011 at 10:46 Comment(5)
Sorry for delay in response but is this the only solution ... is there any possibility of achiving this without main code change at all?Hysterics
@Rahul: If there a way to do it, Moles would be it - have you tried moleing HttpSessionState? This code is factored wrong though, hence why you're having problems testing it. I understand not being able to push it in (I come from test...), but external dependencies (like the fact that it is running in a particular type of web server, and you're accessing global variables) are where I'd make a case for itMilitate
@Rahul: Check also the dupe I found and commented on the main questionMilitate
@Rahul: I found a post describing how to mole it. If it doesn't work, I'd push harder to get something like my original recommendation in :)Militate
This time it's perferct solution ... exactly what I wanted. I understand that, the solution you first provided is really great but I am surrounded with stuborn people :). A ton thanks and really appreaciate your help. Sorry, can't case more than one vote else surely would have. Thanks again and Have a great day.Hysterics

© 2022 - 2024 — McMap. All rights reserved.