How to get NUnit tests to recognize the Entity Framework used in main project when using Visual Studio 2019

57 views Asked by At

I have 2 projects in my solution that are in adjacent directories. First one, is the web app that I set as the startup project, and a second one that has the NUnit tests for the web app. When I try to run the NUnit test I get the following error:

System.InvalidOperationException : No connection string named 'HPCEntities' could be found in the application config file.

I have a copy of the web.config in the directory of the NUnit tests project and it is identical to the one on the web app (which works).

Is there some hidden app.config file that I don't know about that needs the connectionString? Where would I look.

I have made copies of the web.config file to eliminate the 'no connection string found' error without success. I made an app.config, which is in the base directory of the NUnits project, that only had the connectionstring and also got no benefit.

I looked under the hood at the .csproj file which had the following only.

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <RootNamespace>HPC_NUnit_Tests</RootNamespace>

    <IsPackable>false</IsPackable>
  </PropertyGroup>

  <ItemGroup>
    <None Remove=".gitignore" />
    <None Remove="Web.config" />
  </ItemGroup>

  <ItemGroup>
    <Content Include="Web.config" />
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="EntityFramework" Version="6.4.4" />
    <PackageReference Include="nunit" Version="3.12.0" />
    <PackageReference Include="NUnit3TestAdapter" Version="3.15.1" />
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.4.0" />
    <PackageReference Include="RhinoMocks" Version="3.6.0" />
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\HPCv4\HPCv4.csproj" />
      
  </ItemGroup>
    
</Project>

The app.config file has the following:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <connectionStrings>
        <add name="HPCEntities" connectionString="metadata=res://*/DataModel.HPCEntity.csdl|res://*/DataModel.HPCEntity.ssdl|res://*/DataModel.HPCEntity.msl;provider=System.Data.SqlClient;provider connection string=&quot;data.../>
    </connectionStrings>
</configuration>

Just for clarification, my setup method has the following:

        private IMasterWipRepository repo;

        [SetUp]
        public void SetUp()
        {
            HPCEntityContext entity = new HPCEntityContext();
            IRepositoryFactory repoFactory = RepositoryFactory.GetTest(entity);
            repo = repoFactory.GetMasterWipRepo();
        }

What is missing or incorrect?

0

There are 0 answers