Simplifying MvcTestFixture

This commit is contained in:
Pranav K 2015-12-19 10:57:20 -08:00
parent bb666a04cc
commit f56cf97805
3 changed files with 10 additions and 66 deletions

View File

@ -8,10 +8,10 @@ using Microsoft.Extensions.WebEncoders.Testing;
namespace Microsoft.AspNet.Mvc.FunctionalTests
{
public class MvcEncodedTestFixture<TStartup> : MvcTestFixture<TStartup>
where TStartup : new()
{
protected override void AddAdditionalServices(IServiceCollection services)
protected override void InitializeServices(IServiceCollection services)
{
base.InitializeServices(services);
services.AddTransient<HtmlEncoder, HtmlTestEncoder>();
services.AddTransient<JavaScriptEncoder, JavaScriptTestEncoder>();
services.AddTransient<UrlEncoder, UrlTestEncoder>();

View File

@ -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<TStartup> : IDisposable
{
private readonly TestServer _server;
public MvcTestFixture(object startupInstance)
public MvcTestFixture()
{
var startupTypeInfo = startupInstance.GetType().GetTypeInfo();
var configureApplication = (Action<IApplicationBuilder>)startupTypeInfo
.DeclaredMethods
.FirstOrDefault(m => m.Name == "Configure" && m.GetParameters().Length == 1)
?.CreateDelegate(typeof(Action<IApplicationBuilder>), startupInstance);
if (configureApplication == null)
{
var configureWithLogger = (Action<IApplicationBuilder, ILoggerFactory>)startupTypeInfo
.DeclaredMethods
.FirstOrDefault(m => m.Name == "Configure" && m.GetParameters().Length == 2)
?.CreateDelegate(typeof(Action<IApplicationBuilder, ILoggerFactory>), startupInstance);
Debug.Assert(configureWithLogger != null);
configureApplication = application => configureWithLogger(application, NullLoggerFactory.Instance);
}
var configureServices = (Action<IServiceCollection>)startupTypeInfo
.DeclaredMethods
.First(m => m.Name == "ConfigureServices")
.CreateDelegate(typeof(Action<IServiceCollection>), 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<IServiceCollection> 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<IApplicationEnvironment>(
new TestApplicationEnvironment(applicationEnvironment, applicationName, applicationRoot));
var hostingEnvironment = new HostingEnvironment();
hostingEnvironment.Initialize(applicationRoot, new WebApplicationOptions(), configuration: null);
services.AddSingleton<IHostingEnvironment>(hostingEnvironment);
// Inject a custom assembly provider. Overrides AddMvc() because that uses TryAdd().
var assemblyProvider = new StaticAssemblyProvider();
assemblyProvider.CandidateAssemblies.Add(startupAssembly);
services.AddSingleton<IAssemblyProvider>(assemblyProvider);
AddAdditionalServices(services);
if (configureServices != null)
{
configureServices(services);
}
services.AddSingleton<ILoggerFactory>(NullLoggerFactory.Instance);
}
}
}

View File

@ -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<TStartup> : MvcTestFixture
where TStartup : new()
{
public MvcTestFixture()
: base(new TStartup())
{
}
}
}