using AsbCloudDb; using AsbCloudDb.Model; using AsbCloudWebApi.IntegrationTests.TestFakers; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc.Testing; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Xunit; namespace AsbCloudWebApi.IntegrationTests; public class WebAppFactoryFixture : WebApplicationFactory, IAsyncLifetime { protected override void ConfigureWebHost(IWebHostBuilder builder) { var configuration = new ConfigurationBuilder() .AddJsonFile("appsettings.json") .Build(); var connectionString = configuration.GetConnectionString("TestConnection"); builder.ConfigureServices(services => { var descriptor = services.FirstOrDefault(d => d.ServiceType == typeof(DbContextOptions)); if (descriptor is not null) services.Remove(descriptor); services.AddDbContext(options => options.UseNpgsql(connectionString)); }); } public async Task InitializeAsync() { using var scope = Services.CreateScope(); var scopedServices = scope.ServiceProvider; var dbContext = scopedServices.GetRequiredService(); dbContext.Database.EnsureCreatedAndMigrated(); dbContext.Deposits.AddRange(DepositTestFaker.GetDeposits(15)); await dbContext.SaveChangesAsync(); } public new async Task DisposeAsync() { using var scope = Services.CreateScope(); var scopedServices = scope.ServiceProvider; var dbContext = scopedServices.GetRequiredService(); await dbContext.Database.EnsureDeletedAsync(); } }