mardi 23 juin 2020

Asp net core Integration test error when startup read config from appsettings.json

 public class LogClientProvider : IDisposable
{
    private TestServer server;
    public HttpClient Client { get; private set; }

    public LogClientProvider()
    {
        var server = new TestServer(new WebHostBuilder().UseStartup<Startup>());
        Client = server.CreateClient();
        Client.DefaultRequestHeaders.Add("Authorization", RequestHeader.Authorization);
    }

    public void Dispose()
    {
        server?.Dispose();
        Client?.Dispose();
    }
}

This is my Client Provider. And this is integration test service:

  [Fact]
    [Trait("Company","List")]
    public async Task CompanyList()
    {
        using (var client = new LogClientProvider().Client)
        {
            var response = await client.GetAsync("/api/company");
            response.EnsureSuccessStatusCode();
            response.StatusCode.Should().Be(HttpStatusCode.OK);
        }
    }

When I run it Startup.cs file creates error when reading connection string for passing Context(but in project all okay)

services.AddDbContext<LogContext>(options =>
        {
            options.UseSqlServer(Configuration.GetConnectionString("LogDB"));
        });  creates

System.ArgumentNullException: 'Value cannot be null. (Parameter 'connectionString')' error. Configuration.GetConnectionString("LogDB") returns null. How can I solve it?

Aucun commentaire:

Enregistrer un commentaire