2011-10-24 58 views
0

我有一個解決方案與多個項目,其中一個項目是我的服務類,調用到持久性管理器。城堡溫莎配置在多個項目和單元測試

我想如下寫單元測試:

[Test] 
    public void Create_HappyPath_Success() 
    { 
     // Arrange 
     UnitOfMeasure unitOfMeasure = new UnitOfMeasure(); 
     unitOfMeasure.Code = "Some new unit of measure"; 
     unitOfMeasure.DataOwner = 1; 

     // Act 
     this.UoMService.Create(unitOfMeasure); // Fails here as UoMService is null 

     // Assert something 

    } 

現在,我得到這條線上一個空引用異常:

this.UoMService.Create(unitOfMeasure); // Fails here as UoMService is null 

我認爲,這是由於事實上,溫莎城堡沒有被調用,因此UoMService沒有得到實例化。 My Castle Windsor應用程序安裝程序在另一個項目中定義,即我的ASP.NET MVC項目。所以我的第一個問題是是否可以重用該安裝程序來運行我的單元測試。

現在爲了解決這個問題,我在單元測試項目中創建了一個新的安裝程序,鏈接到我的Web項目中的安裝程序。然後我用下面的代碼在我的設立:

[SetUp] 
    public void ControllersInstallerTests() 
    { 
     this.containerWithControllers = new WindsorContainer(); 
     IoC.Initialize(this.containerWithControllers); 

     this.containerWithControllers.Install(FromAssembly.This()); 
    } 

這一次,當我運行測試,我得到以下錯誤:

SetUp : Castle.Windsor.Configuration.Interpreters.XmlProcessor.ConfigurationProcessingException : Error processing node resource FileResource: [] [] ----> Castle.Core.Resource.ResourceException : File C:\Projects\DavidPM\Services\MyProject.Services.ServiceImpl.Test.Unit\bin\Debug\Config\Windsor.config could not be found

的問題是爲什麼它尋找在bin \調試文件夾?作爲溫莎城堡的新手,我不知道我應該怎麼做才能將我的單元測試鉤入溫莎城堡。

回答

1

你應該不是掛鉤你的單元測試你的IoC容器。在生產過程中,您的IoC容器將解決依賴關係。在單元測試過程中,you創建依賴關係作爲測試的一部分 - 通常使用模擬框架,因此您可以單獨測試。

+0

你是對的。感謝那。 – DavidS

0

使你的配置文件複製到輸出目錄