diff --git a/src/ServiceComposer.AspNetCore.Testing/SelfContainedWebApplicationFactoryWithHost.cs b/src/ServiceComposer.AspNetCore.Testing/SelfContainedWebApplicationFactoryWithHost.cs index 2035eb5..77b4b1b 100644 --- a/src/ServiceComposer.AspNetCore.Testing/SelfContainedWebApplicationFactoryWithHost.cs +++ b/src/ServiceComposer.AspNetCore.Testing/SelfContainedWebApplicationFactoryWithHost.cs @@ -7,25 +7,16 @@ namespace ServiceComposer.AspNetCore.Testing { - public class SelfContainedWebApplicationFactoryWithHost : + public class SelfContainedWebApplicationFactoryWithHost(Action configureServices, Action configure, string[] args = null) : WebApplicationFactory where TEntryPoint : class { - private readonly Action configureServices; - private readonly Action configure; - private readonly string[] args; + private readonly string[] args = args ?? new string[0]; public Action HostBuilderCustomization { get; set; } public Action WebHostBuilderCustomization { get; set; } - public SelfContainedWebApplicationFactoryWithHost(Action configureServices, Action configure, string[] args = null) - { - this.configureServices = configureServices; - this.configure = configure; - this.args = args ?? new string[0]; - } - protected override IHostBuilder CreateHostBuilder() { var hostBuilder = Host.CreateDefaultBuilder(args); diff --git a/src/ServiceComposer.AspNetCore.Testing/SelfContainedWebApplicationFactoryWithWebHost.cs b/src/ServiceComposer.AspNetCore.Testing/SelfContainedWebApplicationFactoryWithWebHost.cs index 9068c76..8de198e 100644 --- a/src/ServiceComposer.AspNetCore.Testing/SelfContainedWebApplicationFactoryWithWebHost.cs +++ b/src/ServiceComposer.AspNetCore.Testing/SelfContainedWebApplicationFactoryWithWebHost.cs @@ -7,7 +7,7 @@ namespace ServiceComposer.AspNetCore.Testing { /// - /// Factory for bootstrapping an application in memory for functional end to end tests. + /// Factory for bootstrapping an application in memory for functional end-to-end tests. /// /// /// A type in the entry point assembly of the application. Typically the Startup @@ -20,6 +20,8 @@ public class SelfContainedWebApplicationFactoryWithWebHost : { readonly Action _configureServices; readonly Action _configure; + readonly Action _configureServicesWithWebHostBuilderContext; + readonly Action _configureWithWebHostBuilderContext; public Action BuilderCustomization { get; set; } @@ -28,13 +30,36 @@ public SelfContainedWebApplicationFactoryWithWebHost(Action _configureServices = configureServices; _configure = configure; } + + public SelfContainedWebApplicationFactoryWithWebHost(Action configureServices, Action configure) + { + _configureServicesWithWebHostBuilderContext = configureServices; + _configureWithWebHostBuilderContext = configure; + } protected override IWebHostBuilder CreateWebHostBuilder() { - var host = new WebHostBuilder() - .UseKestrel() - .ConfigureServices(_configureServices) - .Configure(_configure); + var host = new WebHostBuilder().UseKestrel(); + + if (_configureServices != null) + { + host.ConfigureServices(_configureServices); + } + + if (_configureServicesWithWebHostBuilderContext != null) + { + host.ConfigureServices(_configureServicesWithWebHostBuilderContext); + } + + if (_configure != null) + { + host.Configure(_configure); + } + + if (_configureWithWebHostBuilderContext != null) + { + host.Configure(_configureWithWebHostBuilderContext); + } return host; } @@ -42,7 +67,7 @@ protected override IWebHostBuilder CreateWebHostBuilder() protected override void ConfigureWebHost(IWebHostBuilder builder) { base.ConfigureWebHost(builder); - + BuilderCustomization?.Invoke(builder); } }