集成和单元测试不再适用于 ASP.NET 核心 2.1 无法在运行时找到程序集

Integration and unit tests no longer work on ASP.NET Core 2.1 failing to find assemblies at runtime

创建测试项目或升级应用程序并测试到 ASP.NET Core 2.1 / .NET Core 2.1 时,运行 测试失败并出现程序集加载异常,例如

System.IO.FileNotFoundException : Could not load file or assembly 'Microsoft.AspNetCore, Version=2.1.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system cannot find the file specified.

添加对其他一些库的引用时,也会出现构建警告,例如

warning MSB3277: Found conflicts between different versions of "Microsoft.Extensions.Options" that could not be resolved.
warning MSB3277: Found conflicts between different versions of "Microsoft.Extensions.Configuration.Abstractions" that could not be resolved.
warning MSB3277: Found conflicts between different versions of "Microsoft.AspNetCore.Hosting.Abstractions" that could not be resolved.
warning MSB3277: Found conflicts between different versions of "Microsoft.Extensions.DependencyInjection.Abstractions" that could not be resolved.
warning MSB3277: Found conflicts between different versions of "Microsoft.AspNetCore.Http.Abstractions" that could not be resolved.
warning MSB3277: Found conflicts between different versions of "Microsoft.AspNetCore.Http.Features" that could not be resolved.

如何使测试项目用于测试 ASP.NET Core 2.1 应用程序?

更新2.2 工具 使这变得更容易。确保您的 dotnet --version SDK 版本至少为 2.2.100,即使在构建 2.1 应用程序时也是如此

只需在项目中添加无版本包引用,同时保持 Microsoft.NET.Sdk:

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

      <PropertyGroup>
        <TargetFramework>netcoreapp2.1</TargetFramework>
      </PropertyGroup>

      <ItemGroup>
        <PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="2.1.1" />
        <PackageReference Include="Microsoft.AspNetCore.App" />
        <!-- other references to xunit, test SDK etc. -->
      </ItemGroup>

      <ItemGroup>
        <ProjectReference Include="..\AspNetCoreAppToTest\AspNetCoreAppToTest.csproj" />
      </ItemGroup>

    </Project>

原文:

ASP.NET Core 2.1 使用了新的 "shared framework" 到 运行 ASP.NET 核心应用程序。测试项目需要 modified/updated 才能通过以下方法之一使用此共享框架:

  1. 将第一行中测试项目的<Project>标签更改为使用web SDK(Microsoft.NET.Sdk.Web而不是Microsoft.NET.Sdk)并添加对[的包引用=18=](或 .All,如果您在 Web 项目中使用它)不指定版本

    测试项目的项目文件 (.csproj) 现在应该如下所示:

    <Project Sdk="Microsoft.NET.Sdk.Web">
    
      <PropertyGroup>
        <TargetFramework>netcoreapp2.1</TargetFramework>
      </PropertyGroup>
    
      <ItemGroup>
        <PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="2.1.1" />
        <PackageReference Include="Microsoft.AspNetCore.App" />
        <!-- other references to xunit, test SDK etc. -->
      </ItemGroup>
    
      <ItemGroup>
        <ProjectReference Include="..\AspNetCoreAppToTest\AspNetCoreAppToTest.csproj" />
      </ItemGroup>
    
    </Project>
    
  2. 备选方案:保持 Sdk 不变并向共享框架包添加 PackageReference 但指定版本。

    这可以通过简单地添加对 Microsoft.AspNetCore.App 的 NuGet 引用来完成。但是,这可能会导致问题,因为当发布 ASP.NET 核心的新补丁版本时,SDK 可能会选择更新参考,并且更新工具以反映这一点。您需要为每个补丁版本更新 NuGet 参考。

我遇到了同样的问题。将此添加到测试项目解决了问题

 <PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.1.1"/>

问题指定 ASP.NET Core 2.1,但是对于那些 运行 与 ASP.NET Core 2.2 有相同问题的人来说,添加就足够了将以下内容添加到您的测试项目文件中:

<PackageReference Include="Microsoft.AspNetCore.App" />

no need 可以设置 Sdk="Microsoft.NET.Sdk.Web" 并且你应该 添加一个版本到上面的包引用 - 这会导致警告。