J'essaye de tester un 'plugin' pour un ASPNET HttpApplication qui est chargé sur Application_Start
.Comment faire pour tester l'objet HttpApplication avec HttpApplicationState
Le code est quelque chose comme ceci:
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();
}
}
Le problème est que je ne peux pas trouver un bon moyen de tester la HttpApplicationState
principalement parce que la propriété HttpApplication.Application
n'est pas Overridable et il ne semble pas être une classe HttpApplicationBase
dans le System.Web.Abstractions
qui permettrait cela.
J'ai essayé des variantes de ce qui suit, toujours courir dans un barrage routier à chaque fois.
[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...
}
Quelqu'un pourrait-il nous éclairer sur de bonnes approches? C'est le premier test de plus qui reposera sur le fait de pouvoir se moquer d'un HttpApplicationState fonctionnant.
Merci pour cela, vous venez de me sauver une charge de temps en essayant de faire assez exactement la même chose – Nathan