【问题标题】:How to test HttpApplication Object with HttpApplicationState如何使用 HttpApplicationState 测试 HttpApplication 对象
【发布时间】:2009-07-23 04:16:45
【问题描述】:

我正在尝试为加载在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 的第一个测试。

【问题讨论】:

    标签: testing httpapplication


    【解决方案1】:

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

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

    基本上,您可以使用反射为您提供带有HttpApplicationState 对象的HttpApplication 对象,足以测试依赖于此的代码块。

    【讨论】:

    • 感谢您为我节省了大量时间尝试做足够接近完全相同的事情
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-01
    • 2016-05-12
    • 1970-01-01
    • 2016-05-15
    • 1970-01-01
    • 1970-01-01
    • 2018-08-09
    相关资源
    最近更新 更多