call api Test server : .net core API integration tests

amin89

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://docs.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.

amin89

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

API Integration Test for Login controller using Identity server4 in asp.net core

Why do I need Razor pages dependencies to write Integration Tests for a .NET Core Web Api?

Append test project appSettings to ASP.NET Core integration tests

Integration tests with asp.net core (test of controllers without the views)

Api Call Schedule - .Net Core

Seed test data for every test in ASP.NET Core / EF Core / xUnit.NET integration tests

How to do an integration test on my Asp.Net core web api endpoint where I upload a file?

Debug Integration test and web api parallely in single .net core 2.2 in single application?

running asp.net web api application in integration tests

Test Automation for API tests

Integration test with IOptions<> in .NET Core

Cannot call API DELETE in ASP.NET core when running my tests (405 Method Not Allowed) . BUT it works in swagger

How to do integration testing on an external API with ASP.NET Core

Simple injector Web API integration package for .NET core?

Nlog integration for logging in asp.net core API

ASP.NET Core integration tests

.net core integration tests resolve scoped services

Deploying .NET Core Web API to IIS Server

How can I use Asp.Net Core 2.0's in-memory TestServer class for Integration Tests when my API requires an Authorization Token?

Using Autofac in integration tests with web api 2

Routing in Web API is not working in my integration tests

Integration tests for web api with Azure AD

Azure AD: Call .net Core Web API from other .net Core Web API

Integration Test For Hosted Service .Net-Core`

Net Core: Dependency Injection in Integration Test

How to do ASP.NET Web API integration tests with custom authentication and in-memory hosting

How to write Integration Test for Identity Server 4 in asp.net mvc core

Host .Net Core 2.2 web api on Server with .Net framework installed

Call asp.net core web api from PostMan

TOP Ranking

HotTag

Archive