question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to load/reference appsettings.json when integration testing an ASP.NET Core WebApi app using MVC.Testing

See original GitHub issue

Hi 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:closed
  • Created 4 years ago
  • Comments:9 (9 by maintainers)

github_iconTop GitHub Comments

2reactions
pokecommented, Apr 15, 2019

So instead of passing around a DBContext we pass around a ConnectionString

Then you can just inject the connection string POCO there. You have the full DI container, so do whatever you need to do 😉

Also in your test above, that POST will mutate/change the DB. Why aren’t you rolling that back? Or you usually would but this is just some quick and simple example code?

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.

So do you mean something like this?

Actually, I was thinking more about something like this:

public static class TestHelper
{
    public IConfiguration GetTestConfiguration()
        =>  new ConfigurationBuilder()
            .AddJsonFile("integrationsettings.json")
            .Build();
}

// and then inside of the TestApplicationFactory
builder.ConfigureAppConfiguration(config =>
{
    config.AddConfiguration(TestHelper.GetTestConfiguration());
});

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:

[Fact]
public Task AddItem()
{
    // arrange
    var connectionString = TestHelper.GetTestConfiguration().GetConnectionString("DefaultConnection");
    var service = new ExampleService(connectionString);

    // act
    service.Add(new Item { Foo = "Bar" });

    // assert
    using (var db = new SqlConnection(connectionString))
    {
        // verify stuff
    }
}
1reaction
PureKromecommented, Apr 17, 2019

Thanks for the help! Much appreciated. Yep, question has been answered.

Read more comments on GitHub >

github_iconTop Results From Across the Web

AppSettings.json for Integration Test in ASP.NET Core
json file in the test project itself) so the test fails at creating the TestServer . I tried getting around this by modifying...
Read more >
Using custom appsettings.json with ASP.NET Core ...
We have to tweak our test class to make it include correct appsettings.json. As TestServer is run in output folder of integration tests...
Read more >
Overriding configuration in ASP.NET Core integration tests
This post gives an overview of the various ways to override configuration values in ASP.NET Core integration tests.
Read more >
Supporting integration tests with WebApplicationFactory in ...
In this post I look at the changes that were made to WebApplicationFactory to support minimal hosting APIs with WebApplicationBuilder in .
Read more >
deps.json not published for projects referenced for XUnit ...
Iam able to run the XUnit integration tests using Visual Studio. On Release build, the bin folder has both files - Xyz.Api.deps.json and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found