如何使用 HttpApplicationState 测试 HttpApplication 对象
我正在尝试测试在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
经过一些仔细的思考和一些反思:),我想出了这个解决方案来解决我自己的问题:
本质上,你可以使用反射给你一个带有
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:
Essentially you can use reflection to give you an
HttpApplication
Object with aHttpApplicationState
object that will suffice for testing code blocks that rely on this.