C
C#•2w ago
TauFrisch

Populating IOptions in integration tests

Hi, I'm struggling with IOptions when building integration tests specifically, as I'm getting the 'InvalidOperationException: Section 'MySection' not found in configuration' on startup. Those options are mandatory. I know two approach, which both didn't seem to work. First is with AddInMemoryCollection
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
builder.ConfigureAppConfiguration((context, configBuilder) =>
{
var inMemorySettings = new Dictionary<string, string?>
{
{ "ConnectionStrings:DefaultConnection", connectionString },
{ "MySection:SomeAttribute", "Hello there!" },
};
configBuilder.AddInMemoryCollection(inMemorySettings);
});
// ....
}
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
builder.ConfigureAppConfiguration((context, configBuilder) =>
{
var inMemorySettings = new Dictionary<string, string?>
{
{ "ConnectionStrings:DefaultConnection", connectionString },
{ "MySection:SomeAttribute", "Hello there!" },
};
configBuilder.AddInMemoryCollection(inMemorySettings);
});
// ....
}
here I have no idea why I have no problem with the connection string, but with the option. Second approach I tried was to add a testing appsettings:
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
builder.ConfigureAppConfiguration((context, configBuilder) =>
{
var inMemorySettings = new Dictionary<string, string?>
{
{ "ConnectionStrings:DefaultConnection", connectionString }
};
configBuilder.AddInMemoryCollection(inMemorySettings);
configBuilder
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.Integrationtests.json", optional: false, reloadOnChange: true);
});
// ....
}
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
builder.ConfigureAppConfiguration((context, configBuilder) =>
{
var inMemorySettings = new Dictionary<string, string?>
{
{ "ConnectionStrings:DefaultConnection", connectionString }
};
configBuilder.AddInMemoryCollection(inMemorySettings);
configBuilder
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.Integrationtests.json", optional: false, reloadOnChange: true);
});
// ....
}
which I expected to work ... but it didn't either. So I guess I'm making a pretty obvious mistake, but I don't see it. I hope somebody can help me
5 Replies
JakenVeina
JakenVeina•2w ago
well, the first thing I would tell you to do is stop trying to use ConfigureWebHost() in a testing project or are you trying to actually do full-stack testing of your web host? even if you are, I'd say it's prooooooobably a better idea to have dedicated DI for testing for your particular problem, that would allow you to potentially omit .Validate() on the options setups if you're not talking about full-stack testing, you should DEFINITELY take a more direct approach of just building a DI container for just the stuff you need for each test fixture
Unknown User
Unknown User•2w ago
Message Not Public
Sign In & Join Server To View
TauFrisch
TauFrischOP•2w ago
ah, thanks for the answer! It actually seems to work, but with a catch. Now I'm getting: FileNotFoundException: The configuration file 'appsettings.Test.json' was not found and is not optional. The expected physical path was '/Users/abenda/Documen ts/GitHub/attendance-list-backend/tests/ApiIntegrationTests/ApiIntegrationTests/bin/Debug/net9.0/appsettings.Test.json'. can you tell me how I can motivate .net to copy the appsettings to the bin folder on build? 😅
Unknown User
Unknown User•2w ago
Message Not Public
Sign In & Join Server To View
TauFrisch
TauFrischOP•2w ago
perfect, that helep! On Rider its options + enter :catok: thank's for the help!

Did you find this page helpful?