C#C
C#3y ago
no >> body

❔ WebApplicationFactory and integration tests

I've run into an issue with my integration tests and I'm looking for some insight. I'm using WebApplicationFactory and trying to set up a configuration inside it. Here's what it looks like:

protected override void ConfigureWebHost(IWebHostBuilder builder)
{
    // Setting up an in-memory collection for my database configuration here
    builder.ConfigureAppConfiguration(
        (context, config) =>
        {
            config.AddInMemoryCollection(
                    new Dictionary<string, string>
                    {
                        [$"{DatabaseConfiguration.SectionName}:{nameof(DatabaseConfiguration.MasterConnectionString)}"] =
                            connectionString,
                        [$"{DatabaseConfiguration.SectionName}:{nameof(DatabaseConfiguration.ReplicaConnectionString)}"] =
                            connectionString
                    });
            // Adding more services and authentication schemes
        });
}


Now, I have a separate setup in my Program.cs file, where I am adding some services including DbContexts. Here's how it looks:

WebApplicationBuilder builder = WebApplication.CreateBuilder();
// Some services and localization stuff
// ...
builder.Host
    .AddDbContexts<MasterDbContext, ReplicaDbContext>(true, true)
    .AddOpenIddict()
    .AddKafkaProducer<AuditLogProducer>();


My AddDbContexts method is actually adding contexts using UseNpgsql and it takes the connection string from my configuration.
Was this page helpful?