Код: Выделить всё
public class HealthCheckEndpointTests : IClassFixture
{
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);
}
}
Код: Выделить всё
public class ItemsApplicationFactory : WebApplicationFactory
{
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 == typeof(DbContextOptions));
if (dbContextDescriptor != null)
services.Remove(dbContextDescriptor);
var serviceCollection = new ServiceCollection()
.AddEntityFrameworkInMemoryDatabase()
.BuildServiceProvider();
services.AddDbContext(mariaDb =>
{
mariaDb.UseInMemoryDatabase("template");
mariaDb.UseInternalServiceProvider(serviceCollection);
});
});
}
}
Код: Выделить всё
System.ObjectDisposedException : Cannot access a disposed object.
Object name: 'IServiceProvider'
Код: Выделить всё
public class HealthCheckEndpointTests : IClassFixture
{
private readonly ItemsApplicationFactory _factory;
public HealthCheckEndpointTests(ItemsApplicationFactory factory)
{
_factory = factory;
}
public async Task HealthCheck_Test()
{
// Arrange
HttpClient httpClient = _factory.CreateClient();
// Act
await Task.Run(() => Task.CompletedTask);
// Assert
Assert.True(true);
}
}
Почему _factory.CreateClient(); не выдает исключение, но httpClient.GetStringAsync( "/health/live") имеет? И как это исправить?
Подробнее здесь: https://stackoverflow.com/questions/761 ... xception-f