I'm starting to build tests from my api and for that I'm using xunit, but it's not reading my appsettings.Development.json, what's wrong?
namespace CorporateMembership.Test.Integration
{
public class ContestTest
{
private readonly HttpClient _httpClient;
public ContestTest()
{
var server = new TestServer(new WebHostBuilder()
.UseEnvironment("Development")
.UseStartup<Startup>());
_httpClient = server.CreateClient();
}...
By default when you create TestServer it doesn't build configuration and you need to build configuration and pass it to the test server.
public class ContestTest
{
private readonly HttpClient _httpClient;
public ContestTest()
{
var environment = "Development";
var directory = Directory.GetCurrentDirectory();
var configurationBuilder = new ConfigurationBuilder()
.SetBasePath(directory)
.AddJsonFile("appsettings.json")
.AddJsonFile($"appsettings.{environment}.json");
var server = new TestServer(new WebHostBuilder()
.UseEnvironment(environment)
.UseConfiguration(configurationBuilder.Build())
.UseStartup<Startup>());
_httpClient = server.CreateClient();
}
}
Your test project is likely built into a different directory than your main project, so your appsettings.Development.json file is not there.
Load your settings with IConfigurationBuilder in your Startup. You can specify a relative path there through AddJsonFile method.
Also do you really need to start a server in your unit test? You test class functionality, which rarely needs a running server. In order to inject test settings into a class test you could use the options pattern and than in your test provide dummy settings via Options.Create(new MyDummySettings()).