Skip to content

Commit 1b92a0e

Browse files
author
John Luo
committed
Fix Mvc Functional test with runtime compilation
1 parent c91034c commit 1b92a0e

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed

src/Mvc/test/Mvc.FunctionalTests/ErrorPageTests.cs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5+
using System.IO;
56
using System.Linq;
67
using System.Net;
78
using System.Net.Http;
89
using System.Net.Http.Headers;
910
using System.Text.Encodings.Web;
1011
using System.Threading.Tasks;
1112
using Microsoft.AspNetCore.Hosting;
13+
using Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation;
14+
using Microsoft.AspNetCore.TestHost;
1215
using Microsoft.Extensions.DependencyInjection;
1316
using Microsoft.Extensions.Logging;
1417
using Microsoft.Extensions.Logging.Testing;
@@ -27,6 +30,8 @@ public class ErrorPageTests : IClassFixture<MvcTestFixture<ErrorPageMiddlewareWe
2730
"If you're seeing this in a published application, set 'CopyRefAssembliesToPublishDirectory' to true in your project file to ensure files in the refs directory are published.");
2831
private readonly AssemblyTestLog _assemblyTestLog;
2932

33+
private readonly MvcTestFixture<ErrorPageMiddlewareWebSite.Startup> _fixture;
34+
3035
public ErrorPageTests(
3136
MvcTestFixture<ErrorPageMiddlewareWebSite.Startup> fixture,
3237
ITestOutputHelper testOutputHelper)
@@ -41,6 +46,8 @@ public ErrorPageTests(
4146
.CreateDefaultClient();
4247
// These tests want to verify runtime compilation and formatting in the HTML of the error page
4348
Client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/html"));
49+
50+
_fixture = fixture;
4451
}
4552

4653
public HttpClient Client { get; }
@@ -49,12 +56,18 @@ public ErrorPageTests(
4956
public async Task CompilationFailuresAreListedByErrorPageMiddleware()
5057
{
5158
// Arrange
59+
var factory = _fixture.Factories.FirstOrDefault() ?? _fixture.WithWebHostBuilder(b => b.UseStartup<ErrorPageMiddlewareWebSite.Startup>());
60+
factory = factory.WithWebHostBuilder(b => b.ConfigureTestServices(serviceCollection => serviceCollection.Configure<MvcRazorRuntimeCompilationOptions>(ConfigureRuntimeCompilationOptions)));
61+
62+
var client = factory.CreateDefaultClient();
63+
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/html"));
64+
5265
var action = "CompilationFailure";
5366
var expected = "Cannot implicitly convert type &#x27;int&#x27; to &#x27;string&#x27;";
5467
var expectedMediaType = MediaTypeHeaderValue.Parse("text/html; charset=utf-8");
5568

5669
// Act
57-
var response = await Client.GetAsync("http://localhost/" + action);
70+
var response = await client.GetAsync("http://localhost/" + action);
5871

5972
// Assert
6073
Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode);
@@ -63,6 +76,16 @@ public async Task CompilationFailuresAreListedByErrorPageMiddleware()
6376
Assert.Contains($"{action}.cshtml", content);
6477
Assert.Contains(expected, content);
6578
Assert.DoesNotContain(PreserveCompilationContextMessage, content);
79+
80+
static void ConfigureRuntimeCompilationOptions(MvcRazorRuntimeCompilationOptions options)
81+
{
82+
// Workaround for incorrectly generated deps file. The build output has all of the binaries required to compile. We'll grab these and
83+
// add it to the list of assemblies runtime compilation uses.
84+
foreach (var path in Directory.EnumerateFiles(AppContext.BaseDirectory, "*.dll"))
85+
{
86+
options.AdditionalReferencePaths.Add(path);
87+
}
88+
}
6689
}
6790

6891
[Fact]

src/Mvc/test/Mvc.FunctionalTests/RazorBuildTest.cs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,15 @@
33

44
using System;
55
using System.Collections.Generic;
6+
using System.IO;
7+
using System.Linq;
68
using System.Net;
79
using System.Net.Http;
810
using System.Threading.Tasks;
11+
using Microsoft.AspNetCore.Hosting;
12+
using Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation;
13+
using Microsoft.AspNetCore.TestHost;
14+
using Microsoft.Extensions.DependencyInjection;
915
using Xunit;
1016

1117
namespace Microsoft.AspNetCore.Mvc.FunctionalTests
@@ -14,7 +20,20 @@ public class RazorBuildTest : IClassFixture<MvcTestFixture<RazorBuildWebSite.Sta
1420
{
1521
public RazorBuildTest(MvcTestFixture<RazorBuildWebSite.Startup> fixture)
1622
{
17-
Client = fixture.CreateDefaultClient();
23+
var factory = fixture.Factories.FirstOrDefault() ?? fixture.WithWebHostBuilder(b => b.UseStartup<RazorBuildWebSite.Startup>());
24+
factory = factory.WithWebHostBuilder(b => b.ConfigureTestServices(serviceCollection => serviceCollection.Configure<MvcRazorRuntimeCompilationOptions>(ConfigureRuntimeCompilationOptions)));
25+
26+
Client = factory.CreateDefaultClient();
27+
28+
static void ConfigureRuntimeCompilationOptions(MvcRazorRuntimeCompilationOptions options)
29+
{
30+
// Workaround for incorrectly generated deps file. The build output has all of the binaries required to compile. We'll grab these and
31+
// add it to the list of assemblies runtime compilation uses.
32+
foreach (var path in Directory.EnumerateFiles(AppContext.BaseDirectory, "*.dll"))
33+
{
34+
options.AdditionalReferencePaths.Add(path);
35+
}
36+
}
1837
}
1938

2039
public HttpClient Client { get; }
@@ -77,7 +96,7 @@ public async Task RazorViews_AreUpdatedOnChange()
7796
var actual2 = body.Trim();
7897
Assert.NotEqual(expected1, actual2);
7998

80-
// Act - 3
99+
// Act - 3
81100
// With all things being the same, expect a cached compilation
82101
body = await Client.GetStringAsync("/UpdateableViews");
83102

0 commit comments

Comments
 (0)