C
C#17mo 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
});
}
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>();
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.
2 Replies
no >> body
no >> bodyOP17mo ago
public static IHostBuilder AddDbContexts<TMaster, TReplica>(
this IHostBuilder hostBuilder,
bool initializeMaster,
bool initializeReplica)
where TMaster : DbContext
where TReplica : DbContext
=> hostBuilder.ConfigureServices(
(hostContext, services) =>
{
DatabaseConfiguration databaseConfiguration =
hostContext.Configuration.GetSection(DatabaseConfiguration.SectionName).Get<DatabaseConfiguration>() ??
throw new ArgumentNullException(nameof(databaseConfiguration));
// Some DbContext registration stuff
});
public static IHostBuilder AddDbContexts<TMaster, TReplica>(
this IHostBuilder hostBuilder,
bool initializeMaster,
bool initializeReplica)
where TMaster : DbContext
where TReplica : DbContext
=> hostBuilder.ConfigureServices(
(hostContext, services) =>
{
DatabaseConfiguration databaseConfiguration =
hostContext.Configuration.GetSection(DatabaseConfiguration.SectionName).Get<DatabaseConfiguration>() ??
throw new ArgumentNullException(nameof(databaseConfiguration));
// Some DbContext registration stuff
});
Here's the tricky part: my ConfigureAppConfiguration is called after the Program.cs so at the point when I need the connection strings, they are null. I tried to solve this by moving my configuration setup to IHostBuilder.ConfigureAppConfiguration instead of IWebHostBuilder.ConfigureAppConfiguration, but it didn't work. I feel like I'm missing something basic here. Any advice or pointers would be really appreciated. Thanks!
Accord
Accord17mo ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server