Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to test HttpApplication Object with HttpApplicationState
    primarykey
    data
    text
    <p>I am trying to test a 'plugin' for an ASPNET HttpApplication that is loaded on <code>Application_Start</code>.</p> <p>The code is something like this:</p> <pre class="lang-cs prettyprint-override"><code>private HttpApplication application; public void Application_Start(object sender, EventArgs e) { this.application = sender as HttpApplication; StartReportService(); } private void StartReportService() { HandledReports = new SortedList&lt;string, string&gt;(); HandledReports.Add("myindex","myvalue"); } public System.Collections.Generic.SortedList&lt;String, String&gt; HandledReports { get { application.Application.Lock(); var handledReports = (SortedList&lt;String, String&gt;)application.Application["handledReports"]; application.Application.UnLock(); return handledReports; } set { application.Application.Lock(); application.Application["handledReports"] = value; application.Application.UnLock(); } } </code></pre> <p>The problem is that I cannot find a good way to test the <code>HttpApplicationState</code> mainly because the <code>HttpApplication.Application</code> property is not overridable and there does not seem to be a <code>HttpApplicationBase</code> class in the <code>System.Web.Abstractions</code> that would allow this.</p> <p>I have tried variations of the following, always running into a road block each time.</p> <pre class="lang-cs prettyprint-override"><code>[TestMethod()] public void StartReportService_ApplicationStateShouldContainMyIndex() { //No HttpApplicationBase in System.Web.Abstractions, must use Real Object var application = new Mock&lt;HttpApplication&gt;(); //Real object does not have a property of type HttpApplicationStateBase so must use real one? //var applicationStateBase = new Mock&lt;HttpApplicationStateBase&gt;(); //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 =&gt; x.Application, applicationState); var plugin = HttpApplicationPlugin.HttpApplicationPluginInstance; plugin.Application_Start(application.Object,null); ...evaluate result... } </code></pre> <p>Could anyone shed some light on some good approaches for me? This is the first test of more that will rely on being able to have a functioning HttpApplicationState mocked.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload