Make E2E tests even more E2E by using ASP.NET Core apps' BuildWebHost method

This commit is contained in:
Steve Sanderson 2017-12-07 11:57:34 +00:00
parent 0ac789221e
commit 6cad4e3b84
4 changed files with 33 additions and 23 deletions

View File

@ -3,6 +3,7 @@
using Microsoft.AspNetCore; using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
namespace MonoSanity namespace MonoSanity
{ {
@ -15,6 +16,9 @@ namespace MonoSanity
public static IWebHost BuildWebHost(string[] args) => public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args) WebHost.CreateDefaultBuilder(args)
.UseConfiguration(new ConfigurationBuilder()
.AddCommandLine(args)
.Build())
.UseStartup<Startup>() .UseStartup<Startup>()
.Build(); .Build();
} }

View File

@ -9,16 +9,12 @@ namespace Microsoft.Blazor.DevHost.Server
{ {
internal class Program internal class Program
{ {
internal static IWebHost BuildWebHost(string[] args) internal static IWebHost BuildWebHost(string[] args) =>
{ WebHost.CreateDefaultBuilder(args)
var configuration = new ConfigurationBuilder() .UseConfiguration(new ConfigurationBuilder()
.AddCommandLine(args) .AddCommandLine(args)
.Build(); .Build())
return WebHost.CreateDefaultBuilder(args)
.UseConfiguration(configuration)
.UseStartup<Startup>() .UseStartup<Startup>()
.Build(); .Build();
}
} }
} }

View File

@ -1,27 +1,36 @@
// Copyright (c) .NET Foundation. All rights reserved. // 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. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.IO; using System.IO;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
namespace Microsoft.Blazor.E2ETest.Infrastructure.ServerFixtures namespace Microsoft.Blazor.E2ETest.Infrastructure.ServerFixtures
{ {
public class AspNetSiteServerFixture<TStartup> : WebHostServerFixture public class AspNetSiteServerFixture : WebHostServerFixture
where TStartup: class
{ {
public delegate IWebHost BuildWebHost(string[] args);
public BuildWebHost BuildWebHostMethod { get; set; }
protected override IWebHost CreateWebHost() protected override IWebHost CreateWebHost()
{ {
var sampleSitePath = Path.Combine( if (BuildWebHostMethod == null)
FindSolutionDir(), {
"samples", throw new InvalidOperationException(
typeof(TStartup).Assembly.GetName().Name); $"No value was provided for {nameof(BuildWebHostMethod)}");
}
return WebHost.CreateDefaultBuilder() var sampleSitePath = Path.Combine(
.UseStartup<TStartup>() FindSolutionDir(),
.UseContentRoot(sampleSitePath) "samples",
.UseUrls("http://127.0.0.1:0") BuildWebHostMethod.Method.DeclaringType.Assembly.GetName().Name);
.Build();
return BuildWebHostMethod(new[]
{
"--urls", "http://127.0.0.1:0",
"--contentroot", sampleSitePath
});
} }
} }
} }

View File

@ -8,11 +8,12 @@ using Xunit;
namespace Microsoft.Blazor.E2ETest.Tests namespace Microsoft.Blazor.E2ETest.Tests
{ {
public class MonoSanityTest : ServerTestBase<AspNetSiteServerFixture<MonoSanity.Startup>> public class MonoSanityTest : ServerTestBase<AspNetSiteServerFixture>
{ {
public MonoSanityTest(BrowserFixture browserFixture, AspNetSiteServerFixture<MonoSanity.Startup> serverFixture) public MonoSanityTest(BrowserFixture browserFixture, AspNetSiteServerFixture serverFixture)
: base(browserFixture, serverFixture) : base(browserFixture, serverFixture)
{ {
serverFixture.BuildWebHostMethod = MonoSanity.Program.BuildWebHost;
} }
[Fact] [Fact]