From 0f0a388eab7e9bcb67a1db8911246e5fb1aa00f7 Mon Sep 17 00:00:00 2001 From: Martin Costello Date: Thu, 4 Apr 2019 23:21:48 +0100 Subject: [PATCH] Extend WebApplicationFactory (#7414) * Support host-agnostic services access * Support host agnostic access to the IServiceCollection associated with WebApplicationFactory. --- ...ft.AspNetCore.Mvc.Testing.netcoreapp3.0.cs | 1 + .../Mvc.Testing/src/WebApplicationFactory.cs | 12 ++++++ .../TestingInfrastructureInheritanceTests.cs | 12 ++++++ .../TestingInfrastructureTests.cs | 37 +++++++++++++++++++ 4 files changed, 62 insertions(+) diff --git a/src/Mvc/Mvc.Testing/ref/Microsoft.AspNetCore.Mvc.Testing.netcoreapp3.0.cs b/src/Mvc/Mvc.Testing/ref/Microsoft.AspNetCore.Mvc.Testing.netcoreapp3.0.cs index 8e4d5d7f67..069eae8180 100644 --- a/src/Mvc/Mvc.Testing/ref/Microsoft.AspNetCore.Mvc.Testing.netcoreapp3.0.cs +++ b/src/Mvc/Mvc.Testing/ref/Microsoft.AspNetCore.Mvc.Testing.netcoreapp3.0.cs @@ -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> 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; } diff --git a/src/Mvc/Mvc.Testing/src/WebApplicationFactory.cs b/src/Mvc/Mvc.Testing/src/WebApplicationFactory.cs index b817468df7..d9d9072cb9 100644 --- a/src/Mvc/Mvc.Testing/src/WebApplicationFactory.cs +++ b/src/Mvc/Mvc.Testing/src/WebApplicationFactory.cs @@ -79,6 +79,18 @@ namespace Microsoft.AspNetCore.Mvc.Testing } } + /// + /// Gets the created by the server associated with this . + /// + public virtual IServiceProvider Services + { + get + { + EnsureServer(); + return _host?.Services ?? _server.Host.Services; + } + } + /// /// Gets the of factories created from this factory /// by further customizing the when calling diff --git a/src/Mvc/test/Mvc.FunctionalTests/TestingInfrastructureInheritanceTests.cs b/src/Mvc/test/Mvc.FunctionalTests/TestingInfrastructureInheritanceTests.cs index 2da8641640..6bc64ec9cb 100644 --- a/src/Mvc/test/Mvc.FunctionalTests/TestingInfrastructureInheritanceTests.cs +++ b/src/Mvc/test/Mvc.FunctionalTests/TestingInfrastructureInheritanceTests.cs @@ -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(); + + // Assert + Assert.NotNull(factory.Services); + Assert.NotNull(factory.Services.GetService(typeof(IConfiguration))); + } + private class CustomizedFactory : WebApplicationFactory where TEntryPoint : class { public bool GetTestAssembliesCalled { get; private set; } diff --git a/src/Mvc/test/Mvc.FunctionalTests/TestingInfrastructureTests.cs b/src/Mvc/test/Mvc.FunctionalTests/TestingInfrastructureTests.cs index defdab27ca..37b5a24cf2 100644 --- a/src/Mvc/test/Mvc.FunctionalTests/TestingInfrastructureTests.cs +++ b/src/Mvc/test/Mvc.FunctionalTests/TestingInfrastructureTests.cs @@ -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(); + + Assert.NotNull(factory.Services); + Assert.NotNull(factory.Services.GetService(typeof(IConfiguration))); + } + + [Fact] + public void TestingInfrastructure_HasServicesUsingWebHostStartup() + { + var factory = new WebApplicationFactory(); + + Assert.NotNull(factory.Services); + Assert.NotNull(factory.Services.GetService(typeof(IConfiguration))); + } + + [Fact] + public void TestingInfrastructure_HasServicesUsingGenericHostProgram() + { + var factory = new WebApplicationFactory(); + + Assert.NotNull(factory.Services); + Assert.NotNull(factory.Services.GetService(typeof(IConfiguration))); + } + + [Fact] + public void TestingInfrastructure_HasServicesUsingGenericHostStartup() + { + var factory = new WebApplicationFactory(); + + Assert.NotNull(factory.Services); + Assert.NotNull(factory.Services.GetService(typeof(IConfiguration))); + } + private class OverridenService : TestService { public OverridenService()