我有一個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="data source=XYZ\sqlexpress;initial catalog=Equipment;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" 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.
我不這麼認爲。你應該模擬數據庫訪問。測試項目不需要知道另一個項目中的配置。 – Sandman