From 1473625ad4d554678761a7c2f7502e24122f54e0 Mon Sep 17 00:00:00 2001 From: alex289 Date: Wed, 4 Sep 2024 12:32:34 +0200 Subject: [PATCH] feat: Simplify integration test setup --- .../Fixtures/TestFixtureBase.cs | 4 +- .../CleanArchitectureWebApplicationFactory.cs | 52 +++---------------- 2 files changed, 9 insertions(+), 47 deletions(-) diff --git a/CleanArchitecture.IntegrationTests/Fixtures/TestFixtureBase.cs b/CleanArchitecture.IntegrationTests/Fixtures/TestFixtureBase.cs index f5b8290..cc054f9 100644 --- a/CleanArchitecture.IntegrationTests/Fixtures/TestFixtureBase.cs +++ b/CleanArchitecture.IntegrationTests/Fixtures/TestFixtureBase.cs @@ -21,9 +21,7 @@ public class TestFixtureBase } protected virtual void RegisterCustomServicesHandler( - IServiceCollection services, - ServiceProvider serviceProvider, - IServiceProvider scopedServices) + IServiceCollection services) { } } \ No newline at end of file diff --git a/CleanArchitecture.IntegrationTests/Infrastructure/CleanArchitectureWebApplicationFactory.cs b/CleanArchitecture.IntegrationTests/Infrastructure/CleanArchitectureWebApplicationFactory.cs index 848b849..112c170 100644 --- a/CleanArchitecture.IntegrationTests/Infrastructure/CleanArchitectureWebApplicationFactory.cs +++ b/CleanArchitecture.IntegrationTests/Infrastructure/CleanArchitectureWebApplicationFactory.cs @@ -13,9 +13,7 @@ namespace CleanArchitecture.IntegrationTests.Infrastructure; public sealed class CleanArchitectureWebApplicationFactory : WebApplicationFactory { public delegate void RegisterCustomServicesHandler( - IServiceCollection services, - ServiceProvider serviceProvider, - IServiceProvider scopedServices); + IServiceCollection services); private readonly bool _addTestAuthentication; private readonly RegisterCustomServicesHandler? _registerCustomServicesHandler; @@ -31,31 +29,14 @@ public sealed class CleanArchitectureWebApplicationFactory : WebApplicationFacto protected override void ConfigureWebHost(IWebHostBuilder builder) { builder.UseEnvironment("Integration"); - base.ConfigureWebHost(builder); + + var redisPort = GlobalSetupFixture.RedisContainer.GetMappedPublicPort(Configuration.RedisPort); + var rabbitPort = GlobalSetupFixture.RabbitContainer.GetMappedPublicPort(Configuration.RabbitMqPort); - var configuration = new ConfigurationBuilder() - .Build(); - - builder.ConfigureAppConfiguration(configurationBuilder => - { - var redisPort = GlobalSetupFixture.RedisContainer.GetMappedPublicPort(Configuration.RedisPort); - var rabbitPort = GlobalSetupFixture.RabbitContainer.GetMappedPublicPort(Configuration.RabbitMqPort); - - configurationBuilder.AddInMemoryCollection([ - new KeyValuePair( - "ConnectionStrings:DefaultConnection", - GlobalSetupFixture.DatabaseConnectionString), - new KeyValuePair( - "RedisHostName", - $"localhost:{redisPort}"), - new KeyValuePair( - "RabbitMQ:Port", - rabbitPort.ToString()) - ]); - - configuration = configurationBuilder.Build(); - }); + Environment.SetEnvironmentVariable("ConnectionStrings:DefaultConnection", GlobalSetupFixture.DatabaseConnectionString); + Environment.SetEnvironmentVariable("RedisHostName", $"localhost:{redisPort}"); + Environment.SetEnvironmentVariable("RabbitMQ:Port", rabbitPort.ToString()); builder.ConfigureServices(services => { @@ -68,24 +49,7 @@ public sealed class CleanArchitectureWebApplicationFactory : WebApplicationFacto }).AddTestAuthentication(_ => { }); } - var sp = services.BuildServiceProvider(); - - using var scope = sp.CreateScope(); - var scopedServices = scope.ServiceProvider; - - // Readd rabbitmq options to use the correct port - var rabbitMq = new RabbitMqConfiguration(); - configuration.Bind("RabbitMQ", rabbitMq); - services.AddSingleton(rabbitMq); - - // Readd IDistributedCache to replace the memory cache with redis - services.AddStackExchangeRedisCache(options => - { - options.Configuration = configuration["RedisHostName"]; - options.InstanceName = "clean-architecture"; - }); - - _registerCustomServicesHandler?.Invoke(services, sp, scopedServices); + _registerCustomServicesHandler?.Invoke(services); }); } } \ No newline at end of file