Unable to load/reference appsettings.json when integration testing an ASP.NET Core WebApi app using MVC.Testing
See original GitHub issueHi there 👋
I’m trying to do a basic integration test for a very simple ASP.NET Core WebApi project.
I’m leveraging the WebApplicationFactory
class and when I do this, my appsettings.json
file located in my test project is not getting used. The appsettings.development.json
file in the WebApi project (the SUT) is getting used.
I’ve tried to use Source Link/Source Debugging using the MS Symbol Server to step through the WebApplicationFactory
class to figure out why/what. It’s a bit hard to grok.
I even sorta tried to use the TEST_CONTENTROOT_APPNAME
trick/hack I noticed in the source code but I couldn’t get that to work properly (I think i’m not correctly adding this via my custom protected override IWebHostBuilder CreateWebHostBuilder()
method early on in the bootstrapping).
Anyways -> can anyone (repo members?) please provide some clues to how I can use my own appsettings.json
locally in my test project … and not the ones in the SUT.
Why?
SUT appsettings.development.json
: db points to localhost
Test project appsettings.json
: db will point to some ‘dev’ server in the cloud.
Issue Analytics
- State:
- Created 4 years ago
- Comments:9 (9 by maintainers)
Then you can just inject the connection string POCO there. You have the full DI container, so do whatever you need to do 😉
This is an integration test on a separate database which will be created only for the test execution and dropped completely afterwards. So I don’t really care about left over stuff, as long as it does not conflict with other tests. And I try to design my tests in a way that they don’t. But of course, that’s totally up to you.
Actually, I was thinking more about something like this:
That way, I do not need to depend on the full web application factory (which means that it spawns the whole application for integration tests) and can just use the config directly in a less-than-full integration test.
So inside of another test, that is not a full integration test, you could do it like this:
Thanks for the help! Much appreciated. Yep, question has been answered.