首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何用HttpApplicationState测试HttpApplication对象

如何用HttpApplicationState测试HttpApplication对象
EN

Stack Overflow用户
提问于 2009-07-23 04:16:46
回答 1查看 5K关注 0票数 4

我正在尝试测试一个加载在Application_Start上的ASP.NET HttpApplication的‘插件’。

代码如下所示:

代码语言:javascript
复制
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类。

我尝试过以下几种方法,每次都会遇到路障。

代码语言:javascript
复制
[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被模仿。

EN

回答 1

Stack Overflow用户

发布于 2009-08-04 03:22:20

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

代码语言:javascript
复制
 [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对象,该对象足以测试依赖于它的代码块。

票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1169577

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档