Search code examples
c#asp.net-core.net-coreintegration-testingxunit

ASP.NET Core 7.0 integration tests could not find reference


I have the following projects:

XY.Api 
XY.IntegrationTests (I use xUnit)

I added XY.Api reference to the test project and I write a basic test like this:

public class UnitTest1
{
    [Fact]
    public void Test1()
    {
        Assert.Equal(1, AsdController.ASD());
    }
}

The result is the following:

System.IO.FileNotFoundException : Could not load file or assembly 'XY.Api, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified.

Why?

---------- Edit --------

if i run this:

[Fact]
        public void Test1()
        {
            List<string> result = new();
            foreach (var a in AppDomain.CurrentDomain.GetAssemblies())
            {
                result.Add(a.Location + "  --  " + a.FullName);
            }


            ;
            //Assert.Equal(1, UsersController.ASD());
        }

At the result list does not contain XY.Api.dll , but at the bin folder there is XY.Api.dll ...

----- Second update

test project .csproj file:

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

  <PropertyGroup>
    <TargetFramework>net7.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>

    <IsPackable>false</IsPackable>
    <IsTestProject>true</IsTestProject>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
    <PackageReference Include="xunit" Version="2.6.1" />
    <PackageReference Include="xunit.runner.visualstudio" Version="2.5.3">
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
    <PackageReference Include="coverlet.collector" Version="3.2.0">
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\..\XY.Api\XY.Api.csproj" />
  </ItemGroup>


</Project>

Solution

  • 1- Check Project References:Make sure that the XY.IntegrationTests project is correctly linked to the XY.Api project. You can confirm this by examining the .csproj file in your test project, where there should be an entry resembling the following:

    <ItemGroup>
      <ProjectReference Include="..\XY.Api\XY.Api.csproj" />
    </ItemGroup>
    

    2- Also, look into the build settings: Ensure that both projects are set to build in the same configuration, such as Debug or Release. If the build configurations don't match up, it could result in the test project not locating the right build of the API project.