I am using HttpSimulator for unit testing. I am testing one method that calls Response.Redirect deeper in the code chain. I ran into a problem. I get
System.NullReferenceException was unhandled by user code HResult=-2147467261 Message= Object reference not set to an instance of an object. Source=System.Web StackTrace: at System.Web.HttpApplication.CompleteRequest() at System.Web.HttpResponse.End() at System.Web.HttpResponse.Redirect(String url, Boolean endResponse, Boolean permanent) at System.Web.HttpResponse.Redirect(String url, Boolean endResponse)
Then I flipped the HttpApplication type and found the method mentioned:
public void CompleteRequest() { this._stepManager.CompleteRequest(); }
I initialize HttpApplication in one of the following ways:
HttpContext.Current.ApplicationInstance = new HttpApplication(); // or with Mock framework var httpApplicationMock = new Mock<HttpApplication>() var applicationInstance = httpApplicationMock.Object;
As I got from the ASP documentation, _stepManager is responsible for the execution and coordination of the module and the handler. This field is initialized, depending on whether the application is enabled in Classic or Integrated mode.
Then I called in my test:
object stepManager = typeof(HttpApplication).GetField("_stepManager", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(HttpContext.Current.ApplicationInstance);
I got that stepManager is NULL, which is expected due to an exception. StepManager is initialized inside the method:
internal void InitInternal(HttpContext context, HttpApplicationState state, MethodInfo[] handlers)
So, from this, I have no control over how to ensure the initialization of stepManager.
Then I tried the second script . In this case, I tried to initialize ApplicationInstance in a different way.
From the following link to AppHost.cs
I tried to initialize ApplicationInstance as follows:
private static HttpApplication GetApplicationInstance() { var writer = new StringWriter(); var workerRequest = new SimpleWorkerRequest("", "", writer); var httpContext = new HttpContext(workerRequest); return (HttpApplication)getApplicationInstanceMethod.Invoke(null, new object[] { httpContext }); }
and I hit the line of the lat method:
InnerException: System.InvalidOperationException HResult=-2146233079 Message=This method cannot be called during the application pre-start initialization phase. Source=System.Web StackTrace: at System.Web.Compilation.BuildManager.EnsureTopLevelFilesCompiled() at System.Web.Compilation.BuildManager.GetGlobalAsaxTypeInternal() at System.Web.Compilation.BuildManager.GetGlobalAsaxType() at System.Web.HttpApplicationFactory.CompileApplication() at System.Web.HttpApplicationFactory.Init() at System.Web.HttpApplicationFactory.EnsureInited() at System.Web.HttpApplicationFactory.GetApplicationInstance(HttpContext context)
I tried the third scenario . I created a custom response derived from HttpResponseBase that overrided the redirect method. But I ran into a problem how to assign the created HttpContextBase to HttpContext.Current. I saw a tip on Sergeyβs blog :
HttpContext httpContext = httpContextBase.ApplicationInstance.Context;
But it is not possible to set Context to ApplicationInstance. It is zero.
Is it possible to resolve one of these three cases or take a different idea / approach to unit test in my scenario.
Thanks, Rastko