From f56cf9780523ed75ea55a0c24962052be9c254d0 Mon Sep 17 00:00:00 2001 From: Pranav K Date: Sat, 19 Dec 2015 10:57:20 -0800 Subject: [PATCH] Simplifying MvcTestFixture --- .../MvcEncodedTestFixtureOfT.cs | 4 +- .../MvcTestFixture.cs | 58 +++---------------- .../MvcTestFixtureOfT.cs | 14 ----- 3 files changed, 10 insertions(+), 66 deletions(-) delete mode 100644 test/Microsoft.AspNet.Mvc.FunctionalTests/MvcTestFixtureOfT.cs diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/MvcEncodedTestFixtureOfT.cs b/test/Microsoft.AspNet.Mvc.FunctionalTests/MvcEncodedTestFixtureOfT.cs index afb356bda3..a7babf743e 100644 --- a/test/Microsoft.AspNet.Mvc.FunctionalTests/MvcEncodedTestFixtureOfT.cs +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/MvcEncodedTestFixtureOfT.cs @@ -8,10 +8,10 @@ using Microsoft.Extensions.WebEncoders.Testing; namespace Microsoft.AspNet.Mvc.FunctionalTests { public class MvcEncodedTestFixture : MvcTestFixture - where TStartup : new() { - protected override void AddAdditionalServices(IServiceCollection services) + protected override void InitializeServices(IServiceCollection services) { + base.InitializeServices(services); services.AddTransient(); services.AddTransient(); services.AddTransient(); diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/MvcTestFixture.cs b/test/Microsoft.AspNet.Mvc.FunctionalTests/MvcTestFixture.cs index dc6adb57b2..3b4d655472 100644 --- a/test/Microsoft.AspNet.Mvc.FunctionalTests/MvcTestFixture.cs +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/MvcTestFixture.cs @@ -2,60 +2,34 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; -using System.Diagnostics; using System.IO; -using System.Linq; using System.Net.Http; using System.Reflection; -using Microsoft.AspNet.Builder; using Microsoft.AspNet.Hosting; using Microsoft.AspNet.Mvc.Infrastructure; using Microsoft.AspNet.TestHost; using Microsoft.AspNet.Testing; -using Microsoft.Extensions.PlatformAbstractions; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Testing; -using Microsoft.AspNet.Hosting.Internal; +using Microsoft.Extensions.PlatformAbstractions; namespace Microsoft.AspNet.Mvc.FunctionalTests { - public class MvcTestFixture : IDisposable + public class MvcTestFixture : IDisposable { private readonly TestServer _server; - public MvcTestFixture(object startupInstance) + public MvcTestFixture() { - var startupTypeInfo = startupInstance.GetType().GetTypeInfo(); - var configureApplication = (Action)startupTypeInfo - .DeclaredMethods - .FirstOrDefault(m => m.Name == "Configure" && m.GetParameters().Length == 1) - ?.CreateDelegate(typeof(Action), startupInstance); - if (configureApplication == null) - { - var configureWithLogger = (Action)startupTypeInfo - .DeclaredMethods - .FirstOrDefault(m => m.Name == "Configure" && m.GetParameters().Length == 2) - ?.CreateDelegate(typeof(Action), startupInstance); - Debug.Assert(configureWithLogger != null); - - configureApplication = application => configureWithLogger(application, NullLoggerFactory.Instance); - } - - var configureServices = (Action)startupTypeInfo - .DeclaredMethods - .First(m => m.Name == "ConfigureServices") - .CreateDelegate(typeof(Action), startupInstance); - // RequestLocalizationOptions saves the current culture when constructed, potentially changing response // localization i.e. RequestLocalizationMiddleware behavior. Ensure the saved culture // (DefaultRequestCulture) is consistent regardless of system configuration or personal preferences. using (new CultureReplacer()) { var builder = new WebApplicationBuilder() - .Configure(configureApplication) - .ConfigureServices( - services => InitializeServices(startupTypeInfo.Assembly, services, configureServices)); + .ConfigureServices(InitializeServices) + .UseStartup(typeof(TStartup)); _server = new TestServer(builder); } @@ -72,14 +46,7 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests _server.Dispose(); } - protected virtual void AddAdditionalServices(IServiceCollection services) - { - } - - private void InitializeServices( - Assembly startupAssembly, - IServiceCollection services, - Action configureServices) + protected virtual void InitializeServices(IServiceCollection services) { var libraryManager = PlatformServices.Default.LibraryManager; @@ -89,6 +56,7 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests // points to the root folder of the test project. // To compensate, we need to calculate the correct project path and override the application // environment value so that components like the view engine work properly in the context of the test. + var startupAssembly = typeof(TStartup).GetTypeInfo().Assembly; var applicationName = startupAssembly.GetName().Name; var library = libraryManager.GetLibrary(applicationName); var applicationRoot = Path.GetDirectoryName(library.Path); @@ -98,21 +66,11 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests services.AddSingleton( new TestApplicationEnvironment(applicationEnvironment, applicationName, applicationRoot)); - var hostingEnvironment = new HostingEnvironment(); - hostingEnvironment.Initialize(applicationRoot, new WebApplicationOptions(), configuration: null); - services.AddSingleton(hostingEnvironment); - // Inject a custom assembly provider. Overrides AddMvc() because that uses TryAdd(). var assemblyProvider = new StaticAssemblyProvider(); assemblyProvider.CandidateAssemblies.Add(startupAssembly); services.AddSingleton(assemblyProvider); - - AddAdditionalServices(services); - - if (configureServices != null) - { - configureServices(services); - } + services.AddSingleton(NullLoggerFactory.Instance); } } } diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/MvcTestFixtureOfT.cs b/test/Microsoft.AspNet.Mvc.FunctionalTests/MvcTestFixtureOfT.cs deleted file mode 100644 index 39f97d3766..0000000000 --- a/test/Microsoft.AspNet.Mvc.FunctionalTests/MvcTestFixtureOfT.cs +++ /dev/null @@ -1,14 +0,0 @@ -// 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. - -namespace Microsoft.AspNet.Mvc.FunctionalTests -{ - public class MvcTestFixture : MvcTestFixture - where TStartup : new() - { - public MvcTestFixture() - : base(new TStartup()) - { - } - } -}