Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

164
Vistas
.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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda