[Fixes #7541] Per-test class customization should not remove global (per-fixture) customization
This commit is contained in:
parent
e94d77c47f
commit
51784bb2d6
|
|
@ -49,15 +49,9 @@ namespace Microsoft.AspNetCore.Mvc.Testing
|
|||
/// <typeparamref name="TEntryPoint" /> will be loaded as application assemblies.
|
||||
/// </para>
|
||||
/// </summary>
|
||||
public WebApplicationFactory() :
|
||||
this(builder => { }, new WebApplicationFactoryClientOptions())
|
||||
public WebApplicationFactory()
|
||||
{
|
||||
}
|
||||
|
||||
private WebApplicationFactory(Action<IWebHostBuilder> configuration, WebApplicationFactoryClientOptions options)
|
||||
{
|
||||
_configuration = configuration;
|
||||
ClientOptions = options;
|
||||
_configuration = ConfigureWebHost;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -75,7 +69,7 @@ namespace Microsoft.AspNetCore.Mvc.Testing
|
|||
/// <summary>
|
||||
/// Gets the <see cref="WebApplicationFactoryClientOptions"/> used by <see cref="CreateClient()"/>.
|
||||
/// </summary>
|
||||
public WebApplicationFactoryClientOptions ClientOptions { get; }
|
||||
public WebApplicationFactoryClientOptions ClientOptions { get; private set; } = new WebApplicationFactoryClientOptions();
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="WebApplicationFactory{TEntryPoint}"/> with a <see cref="IWebHostBuilder"/>
|
||||
|
|
@ -87,12 +81,16 @@ namespace Microsoft.AspNetCore.Mvc.Testing
|
|||
/// <returns>A new <see cref="WebApplicationFactory{TEntryPoint}"/>.</returns>
|
||||
public WebApplicationFactory<TEntryPoint> WithWebHostBuilder(Action<IWebHostBuilder> configuration)
|
||||
{
|
||||
var factory = new WebApplicationFactory<TEntryPoint>(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<TEntryPoint>
|
||||
{
|
||||
private readonly Func<IWebHostBuilder, TestServer> _createServer;
|
||||
private readonly Func<IWebHostBuilder> _createWebHostBuilder;
|
||||
private readonly Func<IEnumerable<Assembly>> _getTestAssemblies;
|
||||
|
||||
public DelegatedWebApplicationFactory(
|
||||
WebApplicationFactoryClientOptions options,
|
||||
Func<IWebHostBuilder, TestServer> createServer,
|
||||
Func<IWebHostBuilder> createWebHostBuilder,
|
||||
Func<IEnumerable<Assembly>> getTestAssemblies,
|
||||
Action<IWebHostBuilder> 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<Assembly> GetTestAssemblies() => _getTestAssemblies();
|
||||
|
||||
protected override void ConfigureWebHost(IWebHostBuilder builder) => _configuration(builder);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<BasicWebSite.Startup>();
|
||||
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<TEntryPoint> : WebApplicationFactory<TEntryPoint> where TEntryPoint : class
|
||||
{
|
||||
public bool GetTestAssembliesCalled { get; private set; }
|
||||
public bool CreateWebHostBuilderCalled { get; private set; }
|
||||
public bool CreateServerCalled { get; private set; }
|
||||
public IList<string> ConfigureWebHostCalled { get; private set; } = new List<string>();
|
||||
|
||||
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<Assembly> GetTestAssemblies()
|
||||
{
|
||||
GetTestAssembliesCalled = true;
|
||||
return base.GetTestAssemblies();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue