ASP.NET Core 7.0集成测试找不到参考

问题描述 投票:0回答:1

我有以下项目:

XY.Api 
XY.IntegrationTests (I use xUnit)

我添加了

XY.Api
对测试项目的引用,并编写了如下基本测试:

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

结果如下:

System.IO.FileNotFoundException:无法加载文件或程序集“XY.Api,版本= 1.0.0.0,文化=中性,PublicKeyToken = null”。系统找不到指定的文件。

为什么?

---------- 编辑 --------

如果我运行这个:

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

结果列表中不包含 XY.Api.dll ,但 bin 文件夹中有 XY.Api.dll ...

-----第二次更新

测试项目.csproj文件:

<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>
c# asp.net-core .net-core integration-testing xunit
1个回答
0
投票

1- 检查项目引用:确保 XY.IntegrationTests 项目正确链接到 XY.Api 项目。您可以通过检查测试项目中的 .csproj 文件来确认这一点,其中应该有类似于以下内容的条目:

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

2- 另外,请检查构建设置:确保两个项目都设置为在相同配置中构建,例如“调试”或“发布”。如果构建配置不匹配,可能会导致测试项目无法找到 API 项目的正确构建。

© www.soinside.com 2019 - 2024. All rights reserved.