How to test an HttpApplication object using HttpApplicationState

I am trying to test the "plugin" for ASPNET HttpApplication, which loads on Application_Start .

The code looks something like this:

 private HttpApplication application; public void Application_Start(object sender, EventArgs e) { this.application = sender as HttpApplication; StartReportService(); } private void StartReportService() { HandledReports = new SortedList<string, string>(); HandledReports.Add("myindex","myvalue"); } public System.Collections.Generic.SortedList<String, String> HandledReports { get { application.Application.Lock(); var handledReports = (SortedList<String, String>)application.Application["handledReports"]; application.Application.UnLock(); return handledReports; } set { application.Application.Lock(); application.Application["handledReports"] = value; application.Application.UnLock(); } } 

The problem is that I cannot find a good way to test HttpApplicationState mainly because the HttpApplication.Application property HttpApplication.Application not HttpApplication.Application there is no HttpApplication.Application class in the System.Web.Abstractions class that would allow this.

I tried the options for the following: always run the road block every time.

 [TestMethod()] public void StartReportService_ApplicationStateShouldContainMyIndex() { //No HttpApplicationBase in System.Web.Abstractions, must use Real Object var application = new Mock<HttpApplication>(); //Real object does not have a property of type HttpApplicationStateBase so must use real one? //var applicationStateBase = new Mock<HttpApplicationStateBase>(); //real one not creable so HACK get private constructor var ctor = typeof(HttpApplicationState).GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic, null, new Type[] { }, new ParameterModifier[] { }); var applicationState = (HttpApplicationState)ctor.Invoke(new Object[] { }); //fails here, HttpApplication.Application not overridable application.SetupProperty(x => x.Application, applicationState); var plugin = HttpApplicationPlugin.HttpApplicationPluginInstance; plugin.Application_Start(application.Object,null); ...evaluate result... } 

Can anyone shed some light on some good approaches for me? This is the first test of a larger number, which will rely on the fact that he will be able to taunt the functioning HttpApplicationState.

+4
source share
1 answer

Ok with some careful thought and a little reflection :), I came up with this solution for my own problem to make me move:

  [TestMethod()] public void StartReportService_ApplicationStateShouldContainMyIndex() { HttpApplicationPlugin plugin=null; HttpApplication app = null; HttpApplicationState applicationState = null; String tempDir = ""; try { #region "Create HttpApplication and ApplicationState" //No HttpApplicationBase in System.Web.Abstractions, must use Real Object app = new HttpApplication(); //real one not creatable so HACK get private constructor var ctor = typeof(HttpApplicationState).GetConstructor( BindingFlags.Instance | BindingFlags.NonPublic, null, new Type[] { }, new ParameterModifier[] { }); applicationState = (HttpApplicationState)ctor.Invoke(new Object[] { }); //HACK in the state prop for testing var stateProp = typeof(HttpApplication).GetField( "_state", BindingFlags.Instance | BindingFlags.NonPublic); stateProp.SetValue(app, applicationState); #endregion plugin = HttpApplicationPlugin.HttpApplicationPluginInstance; plugin.Application_Start(application.Object,null); 

Essentially, you can use reflection to give you an HttpApplication object with an HttpApplication object, which will be sufficient to check for code blocks that rely on this.

+7
source

All Articles