2014-02-14 57 views
7

我有一個web項目解決方案,我決定將一個單元測試項目添加到解決方案。雖然運行測試他們中的一個失敗,此錯誤:在測試項目應用程序配置文件中找不到connectionString

Result Message: 
Test method DetailsRoleController threw exception: 
System.InvalidOperationException: No connection string named 'EquipmentEntities' could be found in the application config file. 

這裏我的測試腳本:

[TestClass] 
public class RoleControllerTest 
{ 
    RoleController RC = new RoleController(); 
    [TestMethod] 
    public void IndexRoleController() 
    { 
    } 
    [TestMethod] 
    public void DetailsRoleController() 
    { 
     var result = RC.Delete(1); 
     Assert.IsNotNull(result); 
    } 
} 

和控制方法:

public ActionResult Details(int? id) 
    { 
     if (id == null) 
     { 
      return new HttpStatusCodeResult(HttpStatusCode.BadRequest); 
     } 
     Role role = db.Roles.Find(id); 
     if (role == null) 
     { 
      return HttpNotFound(); 
     } 
     return View(role); 
    } 

爲什麼這個測試失敗?

這個測試用例是不是使用來自主項目的connectrionstrings/context運行?

好吧,我編輯我的AppConfig和現在正在尋找這樣的:

<configuration> 
    <appSettings> 

    </appSettings> 
    <entityFramework> 
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" /> 
    <providers> 
     <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /> 
    </providers> 
    </entityFramework> 
    <connectionStrings> 
    <add name="EquipmentEntities" connectionString="metadata=res://*/Models.MagazynModel.csdl|res://*/Models.MagazynModel.ssdl|res://*/Models.MagazynModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=XYZ\sqlexpress;initial catalog=Equipment;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" /> 
    </connectionStrings> 
</configuration> 

但現在我有另一個錯誤:

Result Message: Unable to create instance of class magazynTest.Controllers.RoleControllerTest. Error: System.TypeInitializationException: 
The type initializer for 'System.Data.Entity.Internal.AppConfig' threw an exception. ---> System.Configuration.ConfigurationErrorsException: 
Configuration system failed to initialize ---> System.Configuration.ConfigurationErrorsException: Unrecognized configuration section entityFramework. 
+0

我不這麼認爲。你應該模擬數據庫訪問。測試項目不需要知道另一個項目中的配置。 – Sandman

回答

9

這並不是說將它撿起來,從您的網絡的情況下應用程序默認。

它將在它正在運行的當前項目中查找它。

您必須將其添加到包含連接設置的單元測試項目中的app.config文件中。

另一種方法是將它注入到你的控制器中,這種方式在你的測試中可以傳入一個模擬對象,這樣可以防止永遠不必處理實際的數據庫實例。

如果重新設計,使用存儲庫模式,做法沒有看到你的類的東西象下面這樣:

public class RoleController : Controller 
    { 
     private IRoleRepository roleRepository; 

     // This constructor would not be needed if you were to use IOC container 
     public RoleController() 
     { 
     this.roleRepository = new RoleRepository(new RoleContext()); 
     } 

     public RoleController(IRoleRepository studentRepository) 
     { 
     this.roleRepository = roleRepository; 
     } 
     .... 

然後,您可以模擬出庫象下面這樣:

[TestClass] 
public class RoleControllerTest 
{ 
    private Mock<IRoleRepository> _roleRepository; 

    [SetUp] 
    public void SetUp() 
    { 
    _roleRepository = new Mock<IRoleRepository>(); 

    } 



    [TestMethod] 
    public void IndexRoleController() 
    { 
    } 
    [TestMethod] 
    public void DetailsRoleController() 
    { 
     RoleController RC = new RoleController(_roleRepository.Object); 
     var result = RC.Delete(1); 
     Assert.IsNotNull(result); 
    } 
} 

見這裏獲取更多信息:

http://kakimotonline.com/2011/02/13/tdd-in-asp-net-mvc-applications-with-moq-framework/

+0

我用新數據更新了我的問題。你可以提供一些數據在哪裏找到關於模擬數據庫連接的教程嗎? – szpic

+0

現在爲你添加了一個鏈接,祝你好運。 :) – hutchonoid

4

將您的webConfig中的ConnectionStrings部分複製到測試項目appConfig

+0

做到這一點,但它導致了另一個錯誤 – szpic

+1

@szpic -1對於其他錯誤? –

相關問題