diff --git a/src/BuildingBlocks/TestBase/CustomWebApplicationFactory.cs b/src/BuildingBlocks/TestBase/CustomWebApplicationFactory.cs deleted file mode 100644 index 53256d4..0000000 --- a/src/BuildingBlocks/TestBase/CustomWebApplicationFactory.cs +++ /dev/null @@ -1,53 +0,0 @@ -using BuildingBlocks.MassTransit; -using BuildingBlocks.Web; -using MassTransit; -using Microsoft.AspNetCore.Hosting; -using Microsoft.AspNetCore.Http; -using Microsoft.AspNetCore.Mvc.Testing; -using Microsoft.Extensions.DependencyInjection; -using NSubstitute; - -namespace BuildingBlocks.TestBase; - -public class CustomWebApplicationFactory : WebApplicationFactory - where TEntryPoint : class -{ - public Action TestRegistrationServices { set; get; } - - protected override void ConfigureWebHost(IWebHostBuilder builder) - { - builder.UseEnvironment("test"); - builder.ConfigureServices(services => - { - TestRegistrationServices?.Invoke(services); - services.ReplaceSingleton(AddHttpContextAccessorMock); - services.AddMassTransitTestHarness(x => - { - x.UsingRabbitMq((context, cfg) => - { - var rabbitMqOptions = services.GetOptions("RabbitMq"); - var host = rabbitMqOptions.HostName; - - cfg.Host(host, h => - { - h.Username(rabbitMqOptions.UserName); - h.Password(rabbitMqOptions.Password); - }); - cfg.ConfigureEndpoints(context); - }); - }); - }); - } - - private IHttpContextAccessor AddHttpContextAccessorMock(IServiceProvider serviceProvider) - { - var httpContextAccessorMock = Substitute.For(); - using var scope = serviceProvider.CreateScope(); - httpContextAccessorMock.HttpContext = new DefaultHttpContext {RequestServices = scope.ServiceProvider}; - - httpContextAccessorMock.HttpContext.Request.Host = new HostString("localhost", 6012); - httpContextAccessorMock.HttpContext.Request.Scheme = "http"; - - return httpContextAccessorMock; - } -} diff --git a/src/BuildingBlocks/TestBase/IntegrationTestBase.cs b/src/BuildingBlocks/TestBase/IntegrationTestBase.cs index 3dfc332..04ea21f 100644 --- a/src/BuildingBlocks/TestBase/IntegrationTestBase.cs +++ b/src/BuildingBlocks/TestBase/IntegrationTestBase.cs @@ -2,17 +2,24 @@ using BuildingBlocks.Core.Event; using BuildingBlocks.Core.Model; using BuildingBlocks.EFCore; +using BuildingBlocks.MassTransit; using BuildingBlocks.Mongo; using BuildingBlocks.PersistMessageProcessor; +using BuildingBlocks.Web; using DotNet.Testcontainers.Containers; using Grpc.Net.Client; +using MassTransit; using MassTransit.Testing; using MediatR; +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc.Testing; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; using Mongo2Go; +using NSubstitute; using Respawn; using Serilog; using Xunit; @@ -23,7 +30,7 @@ namespace BuildingBlocks.TestBase; public class IntegrationTestFixture : IDisposable where TEntryPoint : class { - private readonly CustomWebApplicationFactory _factory; + private readonly WebApplicationFactory _factory; private int Timeout => 180; public HttpClient HttpClient => _factory.CreateClient(); @@ -32,6 +39,7 @@ public class IntegrationTestFixture : IDisposable public GrpcChannel Channel => GrpcChannel.ForAddress(HttpClient.BaseAddress!, new GrpcChannelOptions {HttpClient = HttpClient}); + public Action TestRegistrationServices { get; set; } public IServiceProvider ServiceProvider => _factory.Services; public IConfiguration Configuration => _factory.Services.GetRequiredService(); @@ -41,8 +49,31 @@ public class IntegrationTestFixture : IDisposable public IntegrationTestFixture() { - // Ref: https://docs.microsoft.com/en-us/aspnet/core/test/integration-tests?view=aspnetcore-6.0#basic-tests-with-the-default-webapplicationfactory - _factory = new CustomWebApplicationFactory(); + _factory = new WebApplicationFactory() + .WithWebHostBuilder(builder => + { + builder.UseEnvironment("test"); + builder.ConfigureServices(services => + { + TestRegistrationServices?.Invoke(services); + services.ReplaceSingleton(AddHttpContextAccessorMock); + services.AddMassTransitTestHarness(x => + { + x.UsingRabbitMq((context, cfg) => + { + var rabbitMqOptions = services.GetOptions("RabbitMq"); + var host = rabbitMqOptions.HostName; + + cfg.Host(host, h => + { + h.Username(rabbitMqOptions.UserName); + h.Password(rabbitMqOptions.Password); + }); + cfg.ConfigureEndpoints(context); + }); + }); + }); + }); } public void Dispose() @@ -52,7 +83,7 @@ public class IntegrationTestFixture : IDisposable public virtual void RegisterServices(Action services) { - _factory.TestRegistrationServices = services; + TestRegistrationServices = services; } // ref: https://github.com/trbenning/serilog-sinks-xunit @@ -141,6 +172,18 @@ public class IntegrationTestFixture : IDisposable }); }); } + + private IHttpContextAccessor AddHttpContextAccessorMock(IServiceProvider serviceProvider) + { + var httpContextAccessorMock = Substitute.For(); + using var scope = serviceProvider.CreateScope(); + httpContextAccessorMock.HttpContext = new DefaultHttpContext {RequestServices = scope.ServiceProvider}; + + httpContextAccessorMock.HttpContext.Request.Host = new HostString("localhost", 6012); + httpContextAccessorMock.HttpContext.Request.Scheme = "http"; + + return httpContextAccessorMock; + } } public class IntegrationTestFixture : IntegrationTestFixture