diff --git a/AsbCloudWebApi.IntegrationTests/WebAppFactoryFixture.cs b/AsbCloudWebApi.IntegrationTests/WebAppFactoryFixture.cs index b990295b..b3edf988 100644 --- a/AsbCloudWebApi.IntegrationTests/WebAppFactoryFixture.cs +++ b/AsbCloudWebApi.IntegrationTests/WebAppFactoryFixture.cs @@ -12,8 +12,7 @@ using AsbCloudWebApi.IntegrationTests.Converters; namespace AsbCloudWebApi.IntegrationTests; -public class WebAppFactoryFixture : WebApplicationFactory, - IDisposable +public class WebAppFactoryFixture : WebApplicationFactory { private static readonly JsonSerializerOptions JsonSerializerOptions = new() { @@ -24,15 +23,20 @@ public class WebAppFactoryFixture : WebApplicationFactory, private static readonly RefitSettings RefitSettings = new(new SystemTextJsonContentSerializer(JsonSerializerOptions)); - protected override void ConfigureWebHost(IWebHostBuilder builder) + private readonly string connectionString; + + public WebAppFactoryFixture() { var configuration = new ConfigurationBuilder() .AddJsonFile("appsettings.Tests.json") .Build(); - - var dbConnection = configuration.GetSection("DbConnection").Get(); - var connectionString = dbConnection?.GetConnectionString(); - + + var dbConnection = configuration.GetSection("DbConnection").Get()!; + connectionString = dbConnection.GetConnectionString(); + } + + protected override void ConfigureWebHost(IWebHostBuilder builder) + { builder.ConfigureServices(services => { var descriptor = services.SingleOrDefault(d => d.ServiceType == typeof(DbContextOptions)); @@ -55,13 +59,14 @@ public class WebAppFactoryFixture : WebApplicationFactory, }); } - public new void Dispose() + public override async ValueTask DisposeAsync() { - using var scope = Services.CreateScope(); - var scopedServices = scope.ServiceProvider; - var dbContext = scopedServices.GetRequiredService(); + var dbContext = new AsbCloudDbContext( + new DbContextOptionsBuilder() + .UseNpgsql(connectionString) + .Options); - dbContext.Database.EnsureDeleted(); + await dbContext.Database.EnsureDeletedAsync(); } public T GetAuthorizedHttpClient(string uriSuffix)