I had a solution with a web project, and I decided to add the Unit Test project to the project. During test execution, one of them fails with this error:
Result Message: Test method DetailsRoleController threw exception: System.InvalidOperationException: No connection string named 'EquipmentEntities' could be found in the application config file.
Here is my test script:
[TestClass] public class RoleControllerTest { RoleController RC = new RoleController(); [TestMethod] public void IndexRoleController() { } [TestMethod] public void DetailsRoleController() { var result = RC.Delete(1); Assert.IsNotNull(result); } }
and controller method:
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); }
Why does this test fail?
Is this test run using connectrionstrings / context from the main project?
Ok, I edited my appconfig and now watch this:
<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>
but now I have one more error:
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.
szpic
source share