集成和单元测试不再适用于 ASP.NET Core 2.1 无法在运行时找到程序集 [英] Integration and unit tests no longer work on ASP.NET Core 2.1 failing to find assemblies at runtime

查看:20
本文介绍了集成和单元测试不再适用于 ASP.NET Core 2.1 无法在运行时找到程序集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

When creating test projects or upgrading an application and tests to ASP.NET Core 2.1 / .NET Core 2.1, running tests fails with assembly load exceptions like

System.IO.FileNotFoundException:无法加载文件或程序集Microsoft.AspNetCore,版本=2.1.0.0,文化=中性,PublicKeyToken=adb9793829ddae60".系统找不到指定的文件.

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.

当添加对其他一些库的引用时,也会出现像

When adding references to some other libraries there are also build warnings like

警告 MSB3277:发现不同版本的Microsoft.Extensions.Options"之间存在无法解决的冲突.
警告 MSB3277:发现不同版本的Microsoft.Extensions.Configuration.Abstractions"之间存在无法解决的冲突.
警告 MSB3277:发现无法解决的不同版本的Microsoft.AspNetCore.Hosting.Abstractions"之间的冲突.
警告 MSB3277:发现无法解决的不同版本的Microsoft.Extensions.DependencyInjection.Abstractions"之间的冲突.
警告 MSB3277:发现无法解决的不同版本的Microsoft.AspNetCore.Http.Abstractions"之间的冲突.
警告 MSB3277:发现不同版本的Microsoft.AspNetCore.Http.Features"之间存在无法解决的冲突.

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 应用程序?

How can I make test projects work for testing ASP.NET Core 2.1 applications?

推荐答案

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

Update: This has been made easier with 2.2 Tooling. Make sure that your dotnet --version SDK version is at least 2.2.100, even when buidling 2.1 applications

只需在您的项目中添加一个无版本的包引用,同时保留 Microsoft.NET.Sdk:

Just add a versionless package reference to your project while keeping the 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="..AspNetCoreAppToTestAspNetCoreAppToTest.csproj" />
      </ItemGroup>

    </Project>

原文:

ASP.NET Core 2.1 使用新的共享框架"来运行 ASP.NET Core 应用程序.需要使用以下方法之一修改/更新测试项目以使用此共享框架:

ASP.NET Core 2.1 uses a new "shared framework" to run ASP.NET Core applications on. Test projects need to be modified/updated to also use this shared framework using one of the following approaches:

  1. 更改第一行中测试项目的 标记以使用 Web SDK(Microsoft.NET.Sdk.Web 而不是 Microsoft.NET.Sdk) 并添加对 Microsoft.AspNetCore.App 的包引用(或 .All 如果您在 Web 项目中使用它)不指定版本

  1. Change the test project's <Project> tag in the first line to use the web SDK (Microsoft.NET.Sdk.Web instead of Microsoft.NET.Sdk) and add a package reference to Microsoft.AspNetCore.App (or .All if you are using that inside the web project) without specifying a version

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

The project file (.csproj) of the test project should now look like this:

<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="..AspNetCoreAppToTestAspNetCoreAppToTest.csproj" />
  </ItemGroup>

</Project>

  • 替代方案:保留 Sdk 原样,并将 PackageReference 添加到共享框架包,但要指定版本.

  • Alternative: Leave the Sdk as-is and add a PackageReference to the shared framework package but specify a version.

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

    This can be done by simply adding a NuGet reference to Microsoft.AspNetCore.App. However, this may cause issues since the SDK may choose to update the reference when a new patch release of the ASP.NET Core is released and the tooling is updated to reflect this. You will need update the NuGet reference for every patch release.

    这篇关于集成和单元测试不再适用于 ASP.NET Core 2.1 无法在运行时找到程序集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆