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: 
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: 
What should I do now? Is the way I'm initializing HttpContext wrong? please advise.