gpt4 book ai didi

testing - 如何使用 HttpApplicationState 测试 HttpApplication 对象

转载 作者:行者123 更新时间:2023-11-28 19:51:48 25 4
gpt4 key购买 nike

我正在尝试为在 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属性不可覆盖并且似乎没有 HttpApplicationBase System.Web.Abstractions 中的类这将允许这样做。

我尝试了以下变体,每次都遇到障碍。

[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 的更多测试。

最佳答案

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

 [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 对象,足以测试依赖它的代码块。

关于testing - 如何使用 HttpApplicationState 测试 HttpApplication 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1169577/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com