【问题标题】:Unable to load one or more of the requested types. Could not load file or assembly 'Microsoft.VisualStudio.Coverage.CoreLib.Net无法加载一种或多种请求的类型。无法加载文件或程序集“Microsoft.VisualStudio.Coverage.CoreLib.Net
【发布时间】:2021-06-23 18:07:28
【问题描述】:

我为集成测试创建了一个 XUnit 测试,并按照此处的示例进行操作 (Integration Tests in ASP.NET Core) 这会加载 mvc 项目的启动,但会因注入自动映射器的异常而失败。

对这意味着什么有什么想法吗?

[![异常用户未处理][2]][2]

测试类

// WebApplicationFactory<TEntryPoint> is used to create a TestServer for the integration tests.
public class ServiceHttpClientTest : IClassFixture<WebApplicationFactory<Startup>>
{
    private readonly WebApplicationFactory<Startup> _factory;

    public ServiceHttpClientTest(WebApplicationFactory<Startup> factory)
    {
        _factory = factory;
    }

    [Theory]
    [InlineData("/Index")]
    [InlineData("/About")]
    [InlineData("/Contact")]
    [InlineData("/Privacy")]
    public async Task Get_EndpointsReturnSuccessAndCorrectContentType(string url)
    {
        // Arrange
        var client = _factory.CreateClient();

        // Act
        var response = await client.GetAsync(url);

        // Assert
        response.EnsureSuccessStatusCode();  // Status Code 200-299
        Assert.Equal("text/html; charset=utf-8", response.Content.Headers.ContentType.ToString());
    }
}



enter code here

项目文件

【问题讨论】:

  • 你的测试项目的 csproj 文件是什么样的?
  • 更新了我的帖子。我尝试按照 Neil 的回答使用 Test.Sdk 的预览,但给出了同样的错误。
  • 如果您将目标框架更改为net5.0,有什么变化吗?

标签: c# dependency-injection automapper integration xunit


【解决方案1】:

我刚才也遇到了类似的问题。 Microsoft.NET.Test.Sdk 16.9.1 中有一个错误。直接的解决方法是将 nuget 包更新到预览版(直到他们发布新版本)。

源代码:https://github.com/microsoft/vstest/issues/2791

【讨论】:

  • 您的问题是否也与 AutoMapper 注入有关?如果我注释掉该行,错误就会消失。
  • 不,不是。我很高兴你找到了正确的解决方案
【解决方案2】:

似乎问题出在 Auto Mapper 上,它扫描加载的程序集以确定类型。所以我遵循了 Jimmy Bogard 在这篇文章中给出的解决方案:Unable to setup AutoMapper in Unit Test ...

我在演示启动文件中实现了以下内容,并且单元测试运行完美!

services.AddAutoMapper(**typeof(Startup)**);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-02
    • 1970-01-01
    • 1970-01-01
    • 2012-12-31
    相关资源
    最近更新 更多