小编The*_*Dev的帖子

与 WebApplicationFactory 的集成测试因 IServiceProvider 的 ObjectDisposeException 失败

我有一个简单的健康检查测试,如下所示:

public class HealthCheckEndpointTests : IClassFixture<ItemsApplicationFactory>
{
    private readonly ItemsApplicationFactory _factory;

    public HealthCheckEndpointTests(ItemsApplicationFactory factory)
    {
        _factory = factory;
    }

    public async Task HealthCheck_Test()
    {
       // Arrange
       HttpClient httpClient = _factory.CreateClient();

       // Act 
       string response = await httpClient.GetStringAsync("/health/live");

       // Assert
       Assert.Equal("Healthy", response);
    }
}
Run Code Online (Sandbox Code Playgroud)

我的 ItemsApplicationFactory 如下所示:

public class ItemsApplicationFactory : WebApplicationFactory<Program>
{
    protected override void ConfigureWebHost(IWebHostBuilder builder)
    {
        builder.ConfigureKestrel(options => options.AllowSynchronousIO = true);


        builder.ConfigureServices(services =>
        {
            // remove db context options if exists 
            var dbContextDescriptor = services.SingleOrDefault(d => d.ServiceType == …
Run Code Online (Sandbox Code Playgroud)

c# integration-testing xunit asp.net-web-api asp.net-core

5
推荐指数
0
解决办法
532
查看次数