Skip to content

[API Proposal]: WebApplicationFactory.ConfigureHostApplicationBuilder for early test config override #66574

Description

@Youssef1313

Background and Motivation

See #37680 for the background and motivation. This is highly requested user feature.

Proposed API

namespace Microsoft.AspNetCore.Mvc.Testing;

public class WebApplicationFactory<TEntryPoint> : IDisposable, IAsyncDisposable where TEntryPoint : class
{
+    protected virtual void ConfigureHostApplicationBuilder(IHostApplicationBuilder hostApplicationBuilder);
}

Open questions:

  • What instance should be passed to IHostApplicationBuilder? The WebApplicationBuilder or its inner _hostApplicationBuilder? Does it matter that much?

Usage Examples

public class TestServerFixture : WebApplicationFactory<Program>
{
    internal Dictionary<string, string> OverrideConfiguration = new();

    protected override void ConfigureHostApplicationBuilder(IHostApplicationBuilder hostApplicationBuilder)
    {
        base.ConfigureHostApplicationBuilder(hostApplicationBuilder);

        var configuration = hostApplicationBuilder.Configuration;
        var testDir = Path.GetDirectoryName(GetType().Assembly.Location);
        var configLocation = Path.Combine(testDir!, "testsettings.json");

        configuration.Sources.Clear();
        configuration.AddJsonFile(configLocation);
        configuration.AddInMemoryCollection(OverrideConfiguration);
    }
}

Alternative Designs

Risks

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions