我正在尝试测试一个加载在Application_Start上的ASP.NET 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被模仿。
发布于 2009-08-04 03:22:20
好吧,经过一些仔细的思考和一些反思:),我想出了这个解决我自己的问题的方案来让我继续前进:
[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对象,该对象足以测试依赖于它的代码块。
https://stackoverflow.com/questions/1169577
复制相似问题