如何使用 HttpApplicationState 测试 HttpApplication 对象

发布于 2024-07-29 10:56:45 字数 2283 浏览 1 评论 0原文

我正在尝试测试在 Application_Start 上加载的 ASPNET HttpApplication 的“插件”。

代码是这样的:

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();
    }
}

问题是我找不到测试 HttpApplicationState 的好方法,主要是因为 HttpApplication.Application 属性不可重写,并且似乎没有成为 System.Web.Abstractions 中允许这样做的 HttpApplicationBase 类。

我尝试过以下各种变体,但每次总是遇到障碍。

[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...
}

有人能为我提供一些好的方法吗? 这是更多依赖于能够模拟正常运行的 HttpApplicationState 的第一个测试。

I am trying to test a 'plugin' for an ASPNET HttpApplication that is loaded on Application_Start.

The code is 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 the HttpApplicationState mainly because the HttpApplication.Application property is not overridable and there does not seem to be a HttpApplicationBase class in the System.Web.Abstractions that would allow this.

I have tried variations of the following, always running into a road block each 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...
}

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

〆凄凉。 2024-08-05 10:56:45

经过一些仔细的思考和一些反思:),我想出了这个解决方案来解决我自己的问题:

 [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);          

本质上,你可以使用反射给你一个带有 HttpApplication 对象>HttpApplicationState 对象足以测试依赖于此的代码块。

Well with some careful thought and a bit of reflection :) , I came up with this solution to my own problem to get me going:

 [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 a HttpApplicationState object that will suffice for testing code blocks that rely on this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文