amin89
amin89

Reputation: 437

call api Test server : .net core API integration tests

I Have an API (.net core) and a local application based on Angular. I want to do integration tests for this API without testing it directly by client side. I checked the .net core docs and I found this : https://learn.microsoft.com/en-us/aspnet/core/testing/integration-testing

That's what I've done :

public class Customers
{
    private readonly TestServer _server;
    private readonly HttpClient _client;
    public Customers()
    {
        // Arrange
        _server = new TestServer(new WebHostBuilder().UseStartup<Startup>());
        _client = _server.CreateClient();
    }

    [Fact]
    public async Task ReturnSomething()
    {
        // Act
        var response = await _client.GetAsync("/api/customers/test");
        response.EnsureSuccessStatusCode();

        var responseString = await response.Content.ReadAsStringAsync();

        // Assert
        Assert.Equal("test",
            responseString);
    }
}

I have this response : Response status code does not indicate success : 500 (internal server error)

When I put "await _client.GetAsync("/api/customers/testtttt" => an incorrect link to API, it display : 404 (not found) so I assume it can join the API.

Why 500 internal error? and how can I have 200 success?

Thank you in advance.

Upvotes: 2

Views: 1147

Answers (1)

amin89
amin89

Reputation: 437

Okay I found the response to this issue. It was a problem with the connection to database (connectionString is null). The API is called and exceptions are generated for 404 & 500 status but not logs for a bad connectivity with BD).

Upvotes: 1

Related Questions