为什么我需要 Razor 页面依赖项来为 .NET Core Web Api 编写集成测试?

Why do I need Razor pages dependencies to write Integration Tests for a .NET Core Web Api?

我正在尝试为我正在构建的 .NET Core Web 编写集成测试Api。

根据找到的文档 Here 我已经通过 Nuget 引用了包 Microsoft.AspNetCore.AppMicrosoft.AspNetCore.Mvc.Testing

但是,当我尝试 运行 我的测试时,出现异常并显示以下消息:

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

这听起来像是在抱怨,因为缺少 Razor 页面依赖项?我的 Api 不使用 Razor 页面。为什么我需要这种依赖?我做错了什么吗?

作为相关附注:Microsoft.AspNetCore.Mvc.TestingMicrosoft.AspNetCore.TestHost 之间有什么不同?前者在我上面链接的文档页面上提到,后者在同一页面的 Test Controllers 标题下提到,几乎没有解释。

编辑:我已经使用 Nuget 安装错误消息中请求的包,Microsoft.AspNetCore.Razor.Runtime,但我仍然遇到同样的错误。现在我真的很困惑。

编辑 2:根据要求,测试项目的 .csproj 文件。

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

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

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

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore" Version="2.2.0" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="2.2.0" />
    <PackageReference Include="Microsoft.AspNetCore.Razor.Runtime" Version="2.2.0" />
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.8.0" />
    <PackageReference Include="xunit" Version="2.3.1" />
    <PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
  </ItemGroup>

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

</Project>

我的测试设置如下:

public class StartupIntegrationTests
    : IClassFixture<WebApplicationFactory<Startup>>
{
    private readonly WebApplicationFactory<Startup> _webAppFactory;

    public StartupIntegrationTests(WebApplicationFactory<Startup> webAppFactory)
    {
        _webAppFactory = webAppFactory.WithWebHostBuilder(builder => builder
            .UseStartup<Startup>()
            .UseSetting("https_port", "443"));
    }


    [Theory]
    [InlineData("/Users")]
    public async Task ShouldUseHttpsForAllRequestsIfClientDidAutoRedirect(string url)
    {
        var client = _webAppFactory.CreateClient();
        var response = await client.GetAsync(url);

        // Ensure that the returned URL is an https url and that there were no errors
        Assert.Equal(HttpStatusCode.OK, response.StatusCode);
        Assert.StartsWith("https://localhost:443/",
                          response.RequestMessage.RequestUri.OriginalString);
    }


    // Some more tests omitted to keep things short

}

这可能是由于 2.1 和 2.2 之间的版本不匹配:您通过 <TargetFramework>netcoreapp2.1</TargetFramework> 以 .NET Core 2.1 为目标,但您包含了 2.2.0 包。将 NuGet 包降级到 2.1.x 版本或将您的应用程序升级到 2.2.0:

  • here
  • 下载.NET Core 2.2 SDK
  • 将应用程序的目标框架从 netcoreapp.2.1 更新为 netcoreapp2.2<TargetFramework>netcoreapp2.1</TargetFramework>。确保你对你的应用程序 你的测试应用程序
  • 都这样做
  • Microsoft.AspNetCore 的所有包引用更新到最新版本(2.2.0 或更高版本)。

还要确保在您的测试项目中包含对 Microsoft.AspNetCore.App 的包引用,方法是将其包含在您的项目文件中:

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

关于您关于 Microsoft.AspNetCore.Mvc.TestingMicrosoft.AspNetCore.TestHost 的问题:TestHost 命名空间由一组类型(包括 TestServer)组成,可以在 -用于测试目的的内存。 Microsoft.AspNetCore.Mvc.Testing 包提供基础设施,通过使用 WebApplicationFactory class.

来启用应用程序(包括 TestServer)的功能测试