Visual Studio Solution:
(Project 1) I have a ASP .NET Framework API project that uses Entity Framework to access to Database.
(Project 2) Now I created NUnit project that is calling methods of project 1. Some of project1's methods are calling EF methods, so:
- I added nuget package of Entity framework to project 2
- I created an App.config file in project 2
But after executing test methods I get:
Test method Test.SchedulerTesting.TestMethod5 threw exception: System.InvalidOperationException: No connection string named 'MyEntities1' could be found in the application config file.
My app.config (project 2) content:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<connectionStrings>
<add name="MyEntities1" connectionString="provider=System.Data.SqlClient;provider connection string="data source=**********;initial catalog=******;user id=sa;password=**********;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
</configuration>
My Model.Context.cs (project 1) first lines:
public partial class MyEntities1: DbContext
{
public MyEntities1()
: base("name=MyEntities1")
{
}
//...
}
CodePudding user response:
An app.config file applies when your production application is running. When running tests under a test runner, you need to set up a config for use with your tests. It will usually contain the same stuff as your production config.
In your case, you need a config file named something like project2.dll.config
in order for the configuration to be made available to your tests. Visual Studio will not automatically rename an App.config
for you because it doesn't know you want to use NUnit.
Funny thing, the first blog post I ever wrote (2005) talks about this and has many more details: http://charliepoole.org/technical/how-nunit-finds-config-files.html