Skip to content

Test WebApplicationBuilder with WebApplicationFactory #63196

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@ public void TestingInfrastructure_WebHost_WithWebHostBuilderRespectsCustomizatio

// Assert
Assert.Equal(new[] { "ConfigureWebHost", "Customization", "FurtherCustomization" }, factory.ConfigureWebHostCalled.ToArray());
Assert.True(factory.CreateServerCalled);
Assert.False(factory.CreateWebHostBuilderCalled);
Assert.True(factory.CreateServerIWebHostBuilderCalled);
Assert.False(factory.CreateServerWithServiceProviderCalled);
Assert.True(factory.CreateWebHostBuilderCalled);
// GetTestAssemblies is not called when reading content roots from MvcAppManifest
Assert.False(factory.GetTestAssembliesCalled);
Assert.True(factory.CreateHostBuilderCalled);
Assert.True(factory.CreateHostCalled);
Assert.False(factory.CreateHostCalled);
}

[Fact]
Expand All @@ -48,7 +49,8 @@ public void TestingInfrastructure_GenericHost_WithWithHostBuilderRespectsCustomi
Assert.False(factory.GetTestAssembliesCalled);
Assert.True(factory.CreateHostBuilderCalled);
Assert.True(factory.CreateHostCalled);
Assert.True(factory.CreateServerCalled);
Assert.False(factory.CreateServerIWebHostBuilderCalled);
Assert.True(factory.CreateServerWithServiceProviderCalled);
Assert.False(factory.CreateWebHostBuilderCalled);
}

Expand Down Expand Up @@ -108,6 +110,23 @@ public void TestingInfrastructure_GenericHost_HostDispose()
Assert.True(sink._asyncDisposed);
}

[Fact]
public void TestingInfrastructure_WebApplicationBuilder_RespectsCustomizations()
{
// Arrange
using var factory = new CustomizedFactory<SimpleWebSiteWithWebApplicationBuilder.Program>();
factory.StartServer();

// Assert
Assert.Equal(["ConfigureWebHost"], factory.ConfigureWebHostCalled.ToArray());
Copy link
Preview

Copilot AI Aug 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Use collection expression syntax consistently. The array literal syntax ["ConfigureWebHost"] is being used here, but line 27 uses new[] { ... }. Consider using the newer collection expression syntax throughout for consistency.

Copilot uses AI. Check for mistakes.

Assert.False(factory.GetTestAssembliesCalled);
Assert.True(factory.CreateHostBuilderCalled);
Assert.True(factory.CreateHostCalled);
Assert.False(factory.CreateServerIWebHostBuilderCalled);
Assert.True(factory.CreateServerWithServiceProviderCalled);
Assert.True(factory.CreateWebHostBuilderCalled);
}

private static void ConfigureWebHostBuilder(IWebHostBuilder builder) =>
builder.UseStartup<GenericHostWebSite.Startup>()
.ConfigureServices(s => s.AddScoped<DisposableService>());
Expand All @@ -131,7 +150,8 @@ private class CustomizedFactory<TEntryPoint> : WebApplicationFactory<TEntryPoint
public bool GetTestAssembliesCalled { get; private set; }
public bool CreateWebHostBuilderCalled { get; private set; }
public bool CreateHostBuilderCalled { get; private set; }
public bool CreateServerCalled { get; private set; }
public bool CreateServerIWebHostBuilderCalled { get; private set; }
public bool CreateServerWithServiceProviderCalled { get; private set; }
public bool CreateHostCalled { get; private set; }
public IList<string> ConfigureWebHostCalled { get; private set; } = new List<string>();

Expand All @@ -143,13 +163,13 @@ protected override void ConfigureWebHost(IWebHostBuilder builder)

protected override TestServer CreateServer(IWebHostBuilder builder)
{
CreateServerCalled = true;
CreateServerIWebHostBuilderCalled = true;
return base.CreateServer(builder);
}

protected override TestServer CreateServer(IServiceProvider serviceProvider)
{
CreateServerCalled = true;
CreateServerWithServiceProviderCalled = true;
return base.CreateServer(serviceProvider);
}

Expand Down
22 changes: 10 additions & 12 deletions src/Mvc/test/WebSites/BasicWebSite/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,19 @@ public class Program
{
public static void Main(string[] args)
{
using var host = CreateHostBuilder(args).Build();
using var host = CreateWebHostBuilder(args).Build();
host.Run();
}

// This method now returns IHostBuilder and uses the new pattern with HostBuilder and ConfigureWebHost
public static IHostBuilder CreateHostBuilder(string[] args) =>
new HostBuilder()
.ConfigureWebHost(webHostBuilder =>
{
webHostBuilder
.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<StartupWithoutEndpointRouting>()
.UseKestrel()
.UseIISIntegration();
});
// Using WebHostBuilder to keep some test coverage in WebApplicationFactory tests
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
#pragma warning disable ASPDEPR004 // Type or member is obsolete
new WebHostBuilder()
#pragma warning restore ASPDEPR004 // Type or member is obsolete
.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<StartupWithoutEndpointRouting>()
.UseKestrel()
.UseIISIntegration();
}

public class TestService
Expand Down
Loading