Simplifying MvcTestFixture
This commit is contained in:
parent
bb666a04cc
commit
f56cf97805
|
|
@ -8,10 +8,10 @@ using Microsoft.Extensions.WebEncoders.Testing;
|
||||||
namespace Microsoft.AspNet.Mvc.FunctionalTests
|
namespace Microsoft.AspNet.Mvc.FunctionalTests
|
||||||
{
|
{
|
||||||
public class MvcEncodedTestFixture<TStartup> : MvcTestFixture<TStartup>
|
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<HtmlEncoder, HtmlTestEncoder>();
|
||||||
services.AddTransient<JavaScriptEncoder, JavaScriptTestEncoder>();
|
services.AddTransient<JavaScriptEncoder, JavaScriptTestEncoder>();
|
||||||
services.AddTransient<UrlEncoder, UrlTestEncoder>();
|
services.AddTransient<UrlEncoder, UrlTestEncoder>();
|
||||||
|
|
|
||||||
|
|
@ -2,60 +2,34 @@
|
||||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.Diagnostics;
|
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using Microsoft.AspNet.Builder;
|
|
||||||
using Microsoft.AspNet.Hosting;
|
using Microsoft.AspNet.Hosting;
|
||||||
using Microsoft.AspNet.Mvc.Infrastructure;
|
using Microsoft.AspNet.Mvc.Infrastructure;
|
||||||
using Microsoft.AspNet.TestHost;
|
using Microsoft.AspNet.TestHost;
|
||||||
using Microsoft.AspNet.Testing;
|
using Microsoft.AspNet.Testing;
|
||||||
using Microsoft.Extensions.PlatformAbstractions;
|
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using Microsoft.Extensions.Logging.Testing;
|
using Microsoft.Extensions.Logging.Testing;
|
||||||
using Microsoft.AspNet.Hosting.Internal;
|
using Microsoft.Extensions.PlatformAbstractions;
|
||||||
|
|
||||||
namespace Microsoft.AspNet.Mvc.FunctionalTests
|
namespace Microsoft.AspNet.Mvc.FunctionalTests
|
||||||
{
|
{
|
||||||
public class MvcTestFixture : IDisposable
|
public class MvcTestFixture<TStartup> : IDisposable
|
||||||
{
|
{
|
||||||
private readonly TestServer _server;
|
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
|
// RequestLocalizationOptions saves the current culture when constructed, potentially changing response
|
||||||
// localization i.e. RequestLocalizationMiddleware behavior. Ensure the saved culture
|
// localization i.e. RequestLocalizationMiddleware behavior. Ensure the saved culture
|
||||||
// (DefaultRequestCulture) is consistent regardless of system configuration or personal preferences.
|
// (DefaultRequestCulture) is consistent regardless of system configuration or personal preferences.
|
||||||
using (new CultureReplacer())
|
using (new CultureReplacer())
|
||||||
{
|
{
|
||||||
var builder = new WebApplicationBuilder()
|
var builder = new WebApplicationBuilder()
|
||||||
.Configure(configureApplication)
|
.ConfigureServices(InitializeServices)
|
||||||
.ConfigureServices(
|
.UseStartup(typeof(TStartup));
|
||||||
services => InitializeServices(startupTypeInfo.Assembly, services, configureServices));
|
|
||||||
|
|
||||||
_server = new TestServer(builder);
|
_server = new TestServer(builder);
|
||||||
}
|
}
|
||||||
|
|
@ -72,14 +46,7 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
|
||||||
_server.Dispose();
|
_server.Dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected virtual void AddAdditionalServices(IServiceCollection services)
|
protected virtual void InitializeServices(IServiceCollection services)
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
private void InitializeServices(
|
|
||||||
Assembly startupAssembly,
|
|
||||||
IServiceCollection services,
|
|
||||||
Action<IServiceCollection> configureServices)
|
|
||||||
{
|
{
|
||||||
var libraryManager = PlatformServices.Default.LibraryManager;
|
var libraryManager = PlatformServices.Default.LibraryManager;
|
||||||
|
|
||||||
|
|
@ -89,6 +56,7 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
|
||||||
// points to the root folder of the test project.
|
// points to the root folder of the test project.
|
||||||
// To compensate, we need to calculate the correct project path and override the application
|
// 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.
|
// 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 applicationName = startupAssembly.GetName().Name;
|
||||||
var library = libraryManager.GetLibrary(applicationName);
|
var library = libraryManager.GetLibrary(applicationName);
|
||||||
var applicationRoot = Path.GetDirectoryName(library.Path);
|
var applicationRoot = Path.GetDirectoryName(library.Path);
|
||||||
|
|
@ -98,21 +66,11 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
|
||||||
services.AddSingleton<IApplicationEnvironment>(
|
services.AddSingleton<IApplicationEnvironment>(
|
||||||
new TestApplicationEnvironment(applicationEnvironment, applicationName, applicationRoot));
|
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().
|
// Inject a custom assembly provider. Overrides AddMvc() because that uses TryAdd().
|
||||||
var assemblyProvider = new StaticAssemblyProvider();
|
var assemblyProvider = new StaticAssemblyProvider();
|
||||||
assemblyProvider.CandidateAssemblies.Add(startupAssembly);
|
assemblyProvider.CandidateAssemblies.Add(startupAssembly);
|
||||||
services.AddSingleton<IAssemblyProvider>(assemblyProvider);
|
services.AddSingleton<IAssemblyProvider>(assemblyProvider);
|
||||||
|
services.AddSingleton<ILoggerFactory>(NullLoggerFactory.Instance);
|
||||||
AddAdditionalServices(services);
|
|
||||||
|
|
||||||
if (configureServices != null)
|
|
||||||
{
|
|
||||||
configureServices(services);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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())
|
|
||||||
{
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Loading…
Reference in New Issue