Skip to content

Commit 887e41d

Browse files
Merge pull request #209 from ServiceComposer/expose-configuration
Allow accessing the configuration
2 parents 9e61f2f + 547f6b1 commit 887e41d

2 files changed

Lines changed: 33 additions & 17 deletions

File tree

src/ServiceComposer.AspNetCore.Testing/SelfContainedWebApplicationFactoryWithHost.cs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,16 @@
77

88
namespace ServiceComposer.AspNetCore.Testing
99
{
10-
public class SelfContainedWebApplicationFactoryWithHost<TEntryPoint> :
10+
public class SelfContainedWebApplicationFactoryWithHost<TEntryPoint>(Action<IServiceCollection> configureServices, Action<IApplicationBuilder> configure, string[] args = null) :
1111
WebApplicationFactory<TEntryPoint>
1212
where TEntryPoint : class
1313
{
14-
private readonly Action<IServiceCollection> configureServices;
15-
private readonly Action<IApplicationBuilder> configure;
16-
private readonly string[] args;
14+
private readonly string[] args = args ?? new string[0];
1715

1816
public Action<IHostBuilder> HostBuilderCustomization { get; set; }
1917

2018
public Action<IWebHostBuilder> WebHostBuilderCustomization { get; set; }
2119

22-
public SelfContainedWebApplicationFactoryWithHost(Action<IServiceCollection> configureServices, Action<IApplicationBuilder> configure, string[] args = null)
23-
{
24-
this.configureServices = configureServices;
25-
this.configure = configure;
26-
this.args = args ?? new string[0];
27-
}
28-
2920
protected override IHostBuilder CreateHostBuilder()
3021
{
3122
var hostBuilder = Host.CreateDefaultBuilder(args);

src/ServiceComposer.AspNetCore.Testing/SelfContainedWebApplicationFactoryWithWebHost.cs

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
namespace ServiceComposer.AspNetCore.Testing
88
{
99
/// <summary>
10-
/// Factory for bootstrapping an application in memory for functional end to end tests.
10+
/// Factory for bootstrapping an application in memory for functional end-to-end tests.
1111
/// </summary>
1212
/// <typeparam name="TEntryPoint">
1313
/// A type in the entry point assembly of the application. Typically the Startup
@@ -20,6 +20,8 @@ public class SelfContainedWebApplicationFactoryWithWebHost<TEntryPoint> :
2020
{
2121
readonly Action<IServiceCollection> _configureServices;
2222
readonly Action<IApplicationBuilder> _configure;
23+
readonly Action<WebHostBuilderContext, IServiceCollection> _configureServicesWithWebHostBuilderContext;
24+
readonly Action<WebHostBuilderContext, IApplicationBuilder> _configureWithWebHostBuilderContext;
2325

2426
public Action<IWebHostBuilder> BuilderCustomization { get; set; }
2527

@@ -28,21 +30,44 @@ public SelfContainedWebApplicationFactoryWithWebHost(Action<IServiceCollection>
2830
_configureServices = configureServices;
2931
_configure = configure;
3032
}
33+
34+
public SelfContainedWebApplicationFactoryWithWebHost(Action<WebHostBuilderContext, IServiceCollection> configureServices, Action<WebHostBuilderContext, IApplicationBuilder> configure)
35+
{
36+
_configureServicesWithWebHostBuilderContext = configureServices;
37+
_configureWithWebHostBuilderContext = configure;
38+
}
3139

3240
protected override IWebHostBuilder CreateWebHostBuilder()
3341
{
34-
var host = new WebHostBuilder()
35-
.UseKestrel()
36-
.ConfigureServices(_configureServices)
37-
.Configure(_configure);
42+
var host = new WebHostBuilder().UseKestrel();
43+
44+
if (_configureServices != null)
45+
{
46+
host.ConfigureServices(_configureServices);
47+
}
48+
49+
if (_configureServicesWithWebHostBuilderContext != null)
50+
{
51+
host.ConfigureServices(_configureServicesWithWebHostBuilderContext);
52+
}
53+
54+
if (_configure != null)
55+
{
56+
host.Configure(_configure);
57+
}
58+
59+
if (_configureWithWebHostBuilderContext != null)
60+
{
61+
host.Configure(_configureWithWebHostBuilderContext);
62+
}
3863

3964
return host;
4065
}
4166

4267
protected override void ConfigureWebHost(IWebHostBuilder builder)
4368
{
4469
base.ConfigureWebHost(builder);
45-
70+
4671
BuilderCustomization?.Invoke(builder);
4772
}
4873
}

0 commit comments

Comments
 (0)