diff --git a/src/Microsoft.AspNetCore.Mvc.Testing/WebApplicationFactory.cs b/src/Microsoft.AspNetCore.Mvc.Testing/WebApplicationFactory.cs
index 3c9d153e8f..f1780878f9 100644
--- a/src/Microsoft.AspNetCore.Mvc.Testing/WebApplicationFactory.cs
+++ b/src/Microsoft.AspNetCore.Mvc.Testing/WebApplicationFactory.cs
@@ -49,15 +49,9 @@ namespace Microsoft.AspNetCore.Mvc.Testing
/// will be loaded as application assemblies.
///
///
- public WebApplicationFactory() :
- this(builder => { }, new WebApplicationFactoryClientOptions())
+ public WebApplicationFactory()
{
- }
-
- private WebApplicationFactory(Action configuration, WebApplicationFactoryClientOptions options)
- {
- _configuration = configuration;
- ClientOptions = options;
+ _configuration = ConfigureWebHost;
}
///
@@ -75,7 +69,7 @@ namespace Microsoft.AspNetCore.Mvc.Testing
///
/// Gets the used by .
///
- public WebApplicationFactoryClientOptions ClientOptions { get; }
+ public WebApplicationFactoryClientOptions ClientOptions { get; private set; } = new WebApplicationFactoryClientOptions();
///
/// Creates a new with a
@@ -87,12 +81,16 @@ namespace Microsoft.AspNetCore.Mvc.Testing
/// A new .
public WebApplicationFactory WithWebHostBuilder(Action configuration)
{
- var factory = new WebApplicationFactory(builder =>
- {
- _configuration(builder);
- configuration(builder);
- },
- new WebApplicationFactoryClientOptions(ClientOptions));
+ var factory = new DelegatedWebApplicationFactory(
+ ClientOptions,
+ CreateServer,
+ CreateWebHostBuilder,
+ GetTestAssemblies,
+ builder =>
+ {
+ _configuration(builder);
+ configuration(builder);
+ });
_derivedFactories.Add(factory);
@@ -111,7 +109,6 @@ namespace Microsoft.AspNetCore.Mvc.Testing
var builder = CreateWebHostBuilder();
SetContentRoot(builder);
- ConfigureWebHost(builder);
_configuration(builder);
_server = CreateServer(builder);
}
@@ -341,5 +338,34 @@ namespace Microsoft.AspNetCore.Mvc.Testing
_server?.Dispose();
}
+
+ private class DelegatedWebApplicationFactory : WebApplicationFactory
+ {
+ private readonly Func _createServer;
+ private readonly Func _createWebHostBuilder;
+ private readonly Func> _getTestAssemblies;
+
+ public DelegatedWebApplicationFactory(
+ WebApplicationFactoryClientOptions options,
+ Func createServer,
+ Func createWebHostBuilder,
+ Func> getTestAssemblies,
+ Action configureWebHost)
+ {
+ ClientOptions = new WebApplicationFactoryClientOptions(options);
+ _createServer = createServer;
+ _createWebHostBuilder = createWebHostBuilder;
+ _getTestAssemblies = getTestAssemblies;
+ _configuration = configureWebHost;
+ }
+
+ protected override TestServer CreateServer(IWebHostBuilder builder) => _createServer(builder);
+
+ protected override IWebHostBuilder CreateWebHostBuilder() => _createWebHostBuilder();
+
+ protected override IEnumerable GetTestAssemblies() => _getTestAssemblies();
+
+ protected override void ConfigureWebHost(IWebHostBuilder builder) => _configuration(builder);
+ }
}
}
diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/TestingInfrastructureInheritanceTests.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/TestingInfrastructureInheritanceTests.cs
new file mode 100644
index 0000000000..02c2e622c3
--- /dev/null
+++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/TestingInfrastructureInheritanceTests.cs
@@ -0,0 +1,65 @@
+// Copyright (c) .NET Foundation. All rights reserved.
+// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
+
+using System.Collections.Generic;
+using System.Linq;
+using System.Reflection;
+using Microsoft.AspNetCore.Hosting;
+using Microsoft.AspNetCore.Mvc.Testing;
+using Microsoft.AspNetCore.TestHost;
+using Xunit;
+
+namespace Microsoft.AspNetCore.Mvc.FunctionalTests
+{
+ public class TestingInfrastructureInheritanceTests
+ {
+ [Fact]
+ public void TestingInfrastructure_WithWebHostBuilderRespectsCustomizations()
+ {
+ // Act
+ var factory = new CustomizedFactory();
+ var customized = factory
+ .WithWebHostBuilder(builder => factory.ConfigureWebHostCalled.Add("Customization"))
+ .WithWebHostBuilder(builder => factory.ConfigureWebHostCalled.Add("FurtherCustomization"));
+ var client = customized.CreateClient();
+
+ // Assert
+ Assert.Equal(new[] { "ConfigureWebHost", "Customization", "FurtherCustomization" }, factory.ConfigureWebHostCalled.ToArray());
+ Assert.True(factory.CreateServerCalled);
+ Assert.True(factory.CreateWebHostBuilderCalled);
+ Assert.True(factory.GetTestAssembliesCalled);
+ }
+
+ private class CustomizedFactory : WebApplicationFactory where TEntryPoint : class
+ {
+ public bool GetTestAssembliesCalled { get; private set; }
+ public bool CreateWebHostBuilderCalled { get; private set; }
+ public bool CreateServerCalled { get; private set; }
+ public IList ConfigureWebHostCalled { get; private set; } = new List();
+
+ protected override void ConfigureWebHost(IWebHostBuilder builder)
+ {
+ ConfigureWebHostCalled.Add("ConfigureWebHost");
+ base.ConfigureWebHost(builder);
+ }
+
+ protected override TestServer CreateServer(IWebHostBuilder builder)
+ {
+ CreateServerCalled = true;
+ return base.CreateServer(builder);
+ }
+
+ protected override IWebHostBuilder CreateWebHostBuilder()
+ {
+ CreateWebHostBuilderCalled = true;
+ return base.CreateWebHostBuilder();
+ }
+
+ protected override IEnumerable GetTestAssemblies()
+ {
+ GetTestAssembliesCalled = true;
+ return base.GetTestAssemblies();
+ }
+ }
+ }
+}