From d6eb9d10f3c4afed08961b3174a715a07463529a Mon Sep 17 00:00:00 2001 From: Alexander Konietzko Date: Sat, 1 Jul 2023 17:17:05 +0200 Subject: [PATCH] Fix integration tests --- CleanArchitecture.Api/Program.cs | 30 +++++++++++-------- .../Fixtures/TestFixtureBase.cs | 6 +--- .../CleanArchitectureWebApplicationFactory.cs | 22 +++++--------- 3 files changed, 26 insertions(+), 32 deletions(-) diff --git a/CleanArchitecture.Api/Program.cs b/CleanArchitecture.Api/Program.cs index b176e0a..6be1f06 100644 --- a/CleanArchitecture.Api/Program.cs +++ b/CleanArchitecture.Api/Program.cs @@ -51,6 +51,22 @@ builder.Services.AddMediatR(cfg => { cfg.RegisterServicesFromAssemblies(typeof(P var app = builder.Build(); +using (var scope = app.Services.CreateScope()) +{ + var services = scope.ServiceProvider; + ApplicationDbContext appDbContext = services.GetRequiredService(); + EventStoreDbContext storeDbContext = services.GetRequiredService(); + DomainNotificationStoreDbContext domainStoreDbContext = services.GetRequiredService(); + + appDbContext.EnsureMigrationsApplied(); + + if (app.Environment.EnvironmentName != "Integration") + { + storeDbContext.EnsureMigrationsApplied(); + domainStoreDbContext.EnsureMigrationsApplied(); + } +} + if (app.Environment.IsDevelopment()) { app.UseSwagger(); @@ -62,25 +78,13 @@ app.UseHttpsRedirection(); app.UseAuthentication(); app.UseAuthorization(); -app.MapControllers(); app.MapHealthChecks("/healthz", new HealthCheckOptions { ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse }); +app.MapControllers(); app.MapGrpcService(); -using (var scope = app.Services.CreateScope()) -{ - var services = scope.ServiceProvider; - var appDbContext = services.GetRequiredService(); - var storeDbContext = services.GetRequiredService(); - var domainStoreDbContext = services.GetRequiredService(); - - appDbContext.EnsureMigrationsApplied(); - storeDbContext.EnsureMigrationsApplied(); - domainStoreDbContext.EnsureMigrationsApplied(); -} - app.Run(); // Needed for integration tests web application factory diff --git a/CleanArchitecture.IntegrationTests/Fixtures/TestFixtureBase.cs b/CleanArchitecture.IntegrationTests/Fixtures/TestFixtureBase.cs index 7c9fe26..d2187f6 100644 --- a/CleanArchitecture.IntegrationTests/Fixtures/TestFixtureBase.cs +++ b/CleanArchitecture.IntegrationTests/Fixtures/TestFixtureBase.cs @@ -12,13 +12,9 @@ public class TestFixtureBase { public TestFixtureBase() { - var projectDir = Directory.GetCurrentDirectory(); - var configPath = Path.Combine(projectDir, "appsettings.Integration.json"); - Factory = new CleanArchitectureWebApplicationFactory( SeedTestData, - RegisterCustomServicesHandler, - configPath); + RegisterCustomServicesHandler); ServerClient = Factory.CreateClient(); ServerClient.Timeout = TimeSpan.FromMinutes(5); diff --git a/CleanArchitecture.IntegrationTests/Infrastructure/CleanArchitectureWebApplicationFactory.cs b/CleanArchitecture.IntegrationTests/Infrastructure/CleanArchitectureWebApplicationFactory.cs index a20e57e..63578e2 100644 --- a/CleanArchitecture.IntegrationTests/Infrastructure/CleanArchitectureWebApplicationFactory.cs +++ b/CleanArchitecture.IntegrationTests/Infrastructure/CleanArchitectureWebApplicationFactory.cs @@ -23,25 +23,19 @@ public sealed class CleanArchitectureWebApplicationFactory : WebApplicationFacto private readonly AddCustomSeedDataHandler? _addCustomSeedDataHandler; private readonly SqliteConnection _connection = new("DataSource=:memory:"); - private readonly string? _environment; private readonly RegisterCustomServicesHandler? _registerCustomServicesHandler; public CleanArchitectureWebApplicationFactory( AddCustomSeedDataHandler? addCustomSeedDataHandler, - RegisterCustomServicesHandler? registerCustomServicesHandler, - string? environment = null) + RegisterCustomServicesHandler? registerCustomServicesHandler) { _addCustomSeedDataHandler = addCustomSeedDataHandler; _registerCustomServicesHandler = registerCustomServicesHandler; - _environment = environment; } protected override void ConfigureWebHost(IWebHostBuilder builder) { - if (!string.IsNullOrWhiteSpace(_environment)) - { - builder.UseEnvironment(_environment); - } + builder.UseEnvironment("Integration"); base.ConfigureWebHost(builder); @@ -53,14 +47,14 @@ public sealed class CleanArchitectureWebApplicationFactory : WebApplicationFacto services.SetupTestDatabase(_connection); services.SetupTestDatabase(_connection); - var sp = services.BuildServiceProvider(); + ServiceProvider sp = services.BuildServiceProvider(); - using var scope = sp.CreateScope(); - var scopedServices = scope.ServiceProvider; + using IServiceScope scope = sp.CreateScope(); + IServiceProvider scopedServices = scope.ServiceProvider; - var applicationDbContext = scopedServices.GetRequiredService(); - var storeDbContext = scopedServices.GetRequiredService(); - var domainStoreDbContext = scopedServices.GetRequiredService(); + ApplicationDbContext applicationDbContext = scopedServices.GetRequiredService(); + EventStoreDbContext storeDbContext = scopedServices.GetRequiredService(); + DomainNotificationStoreDbContext domainStoreDbContext = scopedServices.GetRequiredService(); applicationDbContext.EnsureMigrationsApplied();