[英]No connectionstring named '' could be found in the application config file
[英]No connectionString could be found in the test project application config file
我有一個web項目的解決方案,我決定在解決方案中添加一個單元測試項目。 在運行測試時,其中一個失敗並出現此錯誤:
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.
它不是默認情況下會從您的Web應用程序中提取它的情況。
它將在當前正在運行的項目中查找它。
您必須將其添加到包含連接設置的Unit Test項目中的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/
將ConnectionStrings部分從webConfig
到測試項目appConfig
不要忘記在測試項目中安裝實體框架
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.