![](/img/trans.png)
[英]How can I unit test that Windsor can resolve Asp.net MVC3 controller dependencies?
[英]How can I unit test my controller to make sure Windsor can resolve dependencies when using PerWebRequestLifestyle
我的申請中有以下單元測試:
[TestMethod]
public void Windsor_Can_Resolve_HomeController_Dependencies()
{
// Setup
WindsorContainer container = new WindsorContainer();
container.Install(FromAssembly.Containing<HomeController>());
// Act
container.Kernel.Resolve(typeof(HomeController));
}
這一點是為了確保我沒有任何windsor配置問題,在我訪問該控制器上的操作之前我將無法實現。 問題是我的所有對象注冊都被注冊為PerWebRequestLifestyle
所以我不會遇到我的實體框架數據上下文在Web請求之間共享的問題(這會在運行多個操作時導致錯誤)。
但是,每當我運行此單元測試時,我都會遇到以下異常:
System.InvalidOperationException:HttpContext.Current為null。 PerWebRequestLifestyle只能在ASP.Net中使用
如何在不改變對象注冊命令的生活方式的情況下測試此場景?
我不知道你是否可以在ASP.NET(MVC)之外使用PerWebRequestLifestyle(我認為你不能),但是你可以使用IContributeComponentModelConstruction來修改組件注冊時的生活方式 。
這將使您能夠(集成)測試容器而無需修改任何安裝程序。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.