Extend WebApplicationFactory<T> (#7414)
* Support host-agnostic services access
* Support host agnostic access to the IServiceCollection associated with
WebApplicationFactory<T>.
This commit is contained in:
parent
e863da19d0
commit
0f0a388eab
|
|
@ -26,6 +26,7 @@ namespace Microsoft.AspNetCore.Mvc.Testing
|
|||
public Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactoryClientOptions ClientOptions { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } }
|
||||
public System.Collections.Generic.IReadOnlyList<Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory<TEntryPoint>> Factories { get { throw null; } }
|
||||
public Microsoft.AspNetCore.TestHost.TestServer Server { get { throw null; } }
|
||||
public virtual System.IServiceProvider Services { get { throw null; } }
|
||||
protected virtual void ConfigureClient(System.Net.Http.HttpClient client) { }
|
||||
protected virtual void ConfigureWebHost(Microsoft.AspNetCore.Hosting.IWebHostBuilder builder) { }
|
||||
public System.Net.Http.HttpClient CreateClient() { throw null; }
|
||||
|
|
|
|||
|
|
@ -79,6 +79,18 @@ namespace Microsoft.AspNetCore.Mvc.Testing
|
|||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the <see cref="IServiceProvider"/> created by the server associated with this <see cref="WebApplicationFactory{TEntryPoint}"/>.
|
||||
/// </summary>
|
||||
public virtual IServiceProvider Services
|
||||
{
|
||||
get
|
||||
{
|
||||
EnsureServer();
|
||||
return _host?.Services ?? _server.Host.Services;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the <see cref="IReadOnlyList{WebApplicationFactory}"/> of factories created from this factory
|
||||
/// by further customizing the <see cref="IWebHostBuilder"/> when calling
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ using System.Reflection;
|
|||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Mvc.Testing;
|
||||
using Microsoft.AspNetCore.TestHost;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Xunit;
|
||||
|
||||
|
|
@ -52,6 +53,17 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests
|
|||
Assert.False(factory.CreateWebHostBuilderCalled);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TestingInfrastructure_GenericHost_WithWithHostBuilderHasServices()
|
||||
{
|
||||
// Act
|
||||
var factory = new CustomizedFactory<GenericHostWebSite.Startup>();
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(factory.Services);
|
||||
Assert.NotNull(factory.Services.GetService(typeof(IConfiguration)));
|
||||
}
|
||||
|
||||
private class CustomizedFactory<TEntryPoint> : WebApplicationFactory<TEntryPoint> where TEntryPoint : class
|
||||
{
|
||||
public bool GetTestAssembliesCalled { get; private set; }
|
||||
|
|
|
|||
|
|
@ -14,6 +14,7 @@ using Microsoft.AspNetCore.Hosting;
|
|||
using Microsoft.AspNetCore.Mvc.Testing;
|
||||
using Microsoft.AspNetCore.Mvc.Testing.Handlers;
|
||||
using Microsoft.AspNetCore.TestHost;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using RazorPagesClassLibrary;
|
||||
using Xunit;
|
||||
|
|
@ -131,6 +132,42 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests
|
|||
Assert.Equal("GenericTest", response);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TestingInfrastructure_HasServicesUsingWebHostProgram()
|
||||
{
|
||||
var factory = new WebApplicationFactory<BasicWebSite.Program>();
|
||||
|
||||
Assert.NotNull(factory.Services);
|
||||
Assert.NotNull(factory.Services.GetService(typeof(IConfiguration)));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TestingInfrastructure_HasServicesUsingWebHostStartup()
|
||||
{
|
||||
var factory = new WebApplicationFactory<BasicWebSite.Startup>();
|
||||
|
||||
Assert.NotNull(factory.Services);
|
||||
Assert.NotNull(factory.Services.GetService(typeof(IConfiguration)));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TestingInfrastructure_HasServicesUsingGenericHostProgram()
|
||||
{
|
||||
var factory = new WebApplicationFactory<GenericHostWebSite.Program>();
|
||||
|
||||
Assert.NotNull(factory.Services);
|
||||
Assert.NotNull(factory.Services.GetService(typeof(IConfiguration)));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TestingInfrastructure_HasServicesUsingGenericHostStartup()
|
||||
{
|
||||
var factory = new WebApplicationFactory<GenericHostWebSite.Startup>();
|
||||
|
||||
Assert.NotNull(factory.Services);
|
||||
Assert.NotNull(factory.Services.GetService(typeof(IConfiguration)));
|
||||
}
|
||||
|
||||
private class OverridenService : TestService
|
||||
{
|
||||
public OverridenService()
|
||||
|
|
|
|||
Loading…
Reference in New Issue