0

I'm writing an unit test(using NUnit & MOQ) for an action method MethodUnderTest which uses HttpContext internally to do some functionality. I'm setting up a fake hosting environment by calling InitializeHostingEnvironment where I'm initializing the session like:

public static HttpSessionState InitializeSession()
{
    var httpRequest = new HttpRequest("", "http://localhost/", "");
    var stringWriter = new StringWriter();
    var httpResponse = new HttpResponse(stringWriter);
    var httpContext = new HttpContext(httpRequest, httpResponse);

    HttpContext.Current = httpContext;
    HttpContext.Current.Items.Clear();
    HttpSessionState session = (HttpSessionState)ReflectionHelper.Instantiate(typeof(HttpSessionState), new Type[] { typeof(IHttpSessionState) }, new FakeHttpSessionState());

    HttpContext.Current.Items.Add("AspSession", session);

    return session;
}

public static void InitializeHostingEnvironment(string userName, string password)
{
     // lines of code
     InitializeSession();
}

I'm calling the InitializeHostingEnvironment() from my Test Method like so:

public static void Test_MethodUnderTest()
{
    InitializeHostingEnvironment(UN, PW);
    MethodUnderTest(param1, param2, param3); -- getting exception while trying to execute this line
}

While trying to execute the line MethodUnderTest(param1, param2, param3);, I'm getting an exception - System.ArgumentNullException - Value cannot be null. Parameter name httpBrowserCapabilities. Stack trace is given below: enter image description here

Since the exception says httpBrowserCapabilities is null, I tried to initialize it like HttpContext.Current.Request.Browser = new HttpBrowserCapabilities(); inside the InitializeSession() method, but now, I'm getting another exception: enter image description here

What should I do now? Is the way I'm initializing HttpContext wrong? please advise.

2
  • Why would you test the connector instead the results of the connection? I think you are missconcepting the UT at definition level Commented Dec 3, 2021 at 15:53
  • There may be valid reasons why you need to test what you're testing. But you're not showing us the errors. You're showing us where the exceptions were thrown but you've cut off the actual errors. What if these failing tests really indicate an error in the code? Instead of cutting and pasting pictures it's better to copy text. We don't need the whole stacktrace but at least the beginning - the exception type, message, and where it's being thrown. Commented Dec 3, 2021 at 17:37

1 Answer 1

4

I'm writing an unit test(using NUnit & MOQ) for a method MethodUnderTest which uses HttpContext internally to do some functionality.

Right. Don't do that. The controller should be the only method that accesses the HttpContext, and it should extract the data needed for your MethodUnderTest and use the to write to the HttpContext.

Structuring your code so that each method has a single responsibility is fundamental to writing testable code.

Sign up to request clarification or add additional context in comments.

1 Comment

Hi @stackoverflow.com/users/7297700/david-browne-microsoft apologies for any confusion, but MethodUnderTest is an Action Method. I've edited my question now with the same. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.