No connectionString can be found in the test project application configuration file

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=&quot;data source=XYZ\sqlexpress;initial catalog=Equipment;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" 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. ---> System.Configuration.ConfigurationErrorsException: Configuration system failed to initialize ---> System.Configuration.ConfigurationErrorsException: Unrecognized configuration section entityFramework. 
+7
c # unit-testing asp.net-mvc
source share
2 answers

This is not the case when he selects it from your default web application.

He will look for him in the current project where he is running.

You need to add it to the app.config file in the Unit Test project, which shows the connection settings.

Another approach is to inject it into your controller, so in your tests you can pass a mock object that will not allow you to deal with the actual instance of db at all.

If you re-developed the approach to using the repository template without seeing your classes something like this:

 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; } .... 

Then you can exhaust the repository as shown below:

 [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); } } 

See here for more details:

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

+9
source share

copy the ConnectionStrings section from webConfig to the webConfig test projects

+4
source share

All Articles