Skip to content

IAsyncInitializer lifecycle change broke complex test infrastructure orchestration #4032

@neistow

Description

@neistow

Problem Description

Starting from version 1.5.6, the "Complex Test Infrastructure Orchestration" sample no longer works due to the database container not being initialized before calling ConfigureWebHost.

Code Example

public class InMemoryPostgreSqlDatabase : IAsyncInitializer, IAsyncDisposable
{
    public readonly PostgreSqlContainer Container = new PostgreSqlBuilder()
        .WithUsername("User")
        .WithPassword("Password")
        .WithDatabase("TestDatabase")
        .Build();

    public async Task InitializeAsync() => await Container.StartAsync();

    public async ValueTask DisposeAsync() => await Container.DisposeAsync();
}

public class WebApplicationFactory : WebApplicationFactory<Program>, IAsyncInitializer
{
    [ClassDataSource<InMemoryPostgreSqlDatabase>(Shared = SharedType.PerTestSession)]
    public required InMemoryPostgreSqlDatabase PostgreSql { get; init; }

    public Task InitializeAsync()
    {
        _ = Server;

        return Task.CompletedTask;
    }

    protected override void ConfigureWebHost(IWebHostBuilder builder)
    {
        builder.ConfigureAppConfiguration((context, configBuilder) =>
        {
            configBuilder.AddInMemoryCollection(new Dictionary<string, string?>
            {
                { "PostgreSql:ConnectionString", PostgreSql.Container.GetConnectionString() },
            });
        });
    }
}

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions