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
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:
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
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 fixtureUnknown User•2w ago
Message Not Public
Sign In & Join Server To View
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•2w ago
Message Not Public
Sign In & Join Server To View
perfect, that helep! On Rider its options + enter :catok: thank's for the help!