Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

167
Visualizações
.Net Core 5 Web Api - Xunit not reading my appsettings.Development

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();
        }...
over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

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();
    }
}
over 4 years ago · Santiago Trujillo Relatório

0

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()).

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda