Skip to content

Commit 4c55076

Browse files
committed
Fix tests with outdated expectations
1 parent 400809f commit 4c55076

File tree

2 files changed

+23
-13
lines changed

2 files changed

+23
-13
lines changed

src/Mvc/Mvc.Testing/src/WebApplicationFactory.cs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -256,19 +256,13 @@ private void SetContentRoot(IWebHostBuilder builder)
256256
}
257257
}
258258

259-
if (contentRoot is null)
259+
contentRoot ??= GetContentRootFromAssembly();
260+
if (contentRoot is null || !Directory.Exists(contentRoot))
260261
{
261-
contentRoot = GetContentRootFromAssembly();
262+
contentRoot = AppContext.BaseDirectory;
262263
}
263264

264-
if (contentRoot is not null && Directory.Exists(contentRoot))
265-
{
266-
builder.UseContentRoot(contentRoot);
267-
}
268-
else
269-
{
270-
builder.UseContentRoot(AppContext.BaseDirectory);
271-
}
265+
builder.UseContentRoot(contentRoot);
272266
}
273267

274268
private static string? GetContentRootFromFile(string file)

src/Mvc/test/Mvc.FunctionalTests/TestingInfrastructureInheritanceTests.cs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,13 @@ public void TestingInfrastructure_WebHost_WithWebHostBuilderRespectsCustomizatio
2727
Assert.Equal(new[] { "ConfigureWebHost", "Customization", "FurtherCustomization" }, factory.ConfigureWebHostCalled.ToArray());
2828
Assert.True(factory.CreateServerCalled);
2929
Assert.True(factory.CreateWebHostBuilderCalled);
30-
// GetTestAssemblies is not called when reading content roots from MvcAppManifest
31-
Assert.False(factory.GetTestAssembliesCalled);
30+
31+
// When run locally on the developer machine, the test manifest will be generated,
32+
// and the content root will be read from the manifest file.
33+
// However, when run in CI, the relative path in the metadata will not exist,
34+
// and the content root lookup logic will probe the test assemblies too.
35+
//Assert.False(factory.GetTestAssembliesCalled);
36+
3237
Assert.True(factory.CreateHostBuilderCalled);
3338
Assert.False(factory.CreateHostCalled);
3439
}
@@ -45,7 +50,13 @@ public void TestingInfrastructure_GenericHost_WithWithHostBuilderRespectsCustomi
4550

4651
// Assert
4752
Assert.Equal(new[] { "ConfigureWebHost", "Customization", "FurtherCustomization" }, factory.ConfigureWebHostCalled.ToArray());
48-
Assert.False(factory.GetTestAssembliesCalled);
53+
54+
// When run locally on the developer machine, the test manifest will be generated,
55+
// and the content root will be read from the manifest file.
56+
// However, when run in CI, the relative path in the metadata will not exist,
57+
// and the content root lookup logic will probe the test assemblies too.
58+
//Assert.False(factory.GetTestAssembliesCalled);
59+
4960
Assert.True(factory.CreateHostBuilderCalled);
5061
Assert.True(factory.CreateHostCalled);
5162
Assert.False(factory.CreateServerCalled);
@@ -128,7 +139,12 @@ public void Dispose()
128139

129140
private class CustomizedFactory<TEntryPoint> : WebApplicationFactory<TEntryPoint> where TEntryPoint : class
130141
{
142+
// GetTestAssemblies is not called when reading content roots from MvcAppManifest,
143+
// and the content root specified in the manifest is a path that exists.
144+
// Otherwise, the WebApplicationFactory will try to look for the referenced assemblies which have
145+
// `WebApplicationFactoryContentRootAttribute` applied to them, to extract the content root path from that metadata.
131146
public bool GetTestAssembliesCalled { get; private set; }
147+
132148
public bool CreateWebHostBuilderCalled { get; private set; }
133149
public bool CreateHostBuilderCalled { get; private set; }
134150
public bool CreateServerCalled { get; private set; }

0 commit comments

Comments
 (0)