#if POSTGRES using Events.EF.Data.Postgres; #else using Events.EF.Data.MSSQL; #endif using Events.EF.Models; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc.Testing; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Storage; using Microsoft.EntityFrameworkCore.Diagnostics; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; namespace Events.Tests.IntegrationTests.Infrastructure; public class CustomWebApplicationFactory : WebApplicationFactory { private readonly Action? seed; private readonly InMemoryDatabaseRoot databaseRoot = new(); private readonly string databaseName = Guid.NewGuid().ToString(); public CustomWebApplicationFactory(Action? seed = null) { this.seed = seed; } protected override void ConfigureWebHost(IWebHostBuilder builder) { builder.UseEnvironment("Development"); builder.ConfigureServices(services => { services.RemoveAll(typeof(DbContextOptions)); services.RemoveAll(typeof(IDbContextOptionsConfiguration)); services.RemoveAll(typeof(EventsContext)); services.AddDbContext(options => options .UseInMemoryDatabase(databaseName, databaseRoot) .ConfigureWarnings(warnings => warnings.Ignore(CoreEventId.ManyServiceProvidersCreatedWarning))); var serviceProvider = services.BuildServiceProvider(); using var scope = serviceProvider.CreateScope(); var ctx = scope.ServiceProvider.GetRequiredService(); ctx.Database.EnsureCreated(); seed?.Invoke(ctx); ctx.SaveChanges(); }); } }