调用api测试服务器:.net核心API集成测试 [英] call api Test server : .net core API integration tests

查看:66
本文介绍了调用api测试服务器:.net核心API集成测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个API(.net核心)和一个基于Angular的本地应用程序.我想对此API进行集成测试,而无需直接在客户端进行测试.我检查了.net核心文档,发现了以下内容: https://docs.microsoft.com/zh-CN/aspnet/core/testing/integration-testing

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

这就是我所做的:

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);
    }
}

我有此响应:响应状态代码未指示成功:500(内部服务器错误)

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

当我输入"await _client.GetAsync("/api/customers/testtttt"=>到API的错误链接时,它显示:404(未找到),因此我认为它可以加入API.

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.

为什么会出现500个内部错误?怎样才能有200个成功?

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

谢谢.

推荐答案

好的,我找到了对此问题的回应.与数据库的连接有问题(connectionString为null).调用该API,并为404& amp;生成异常.500状态,但未记录与BD的连接不良).

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

这篇关于调用api测试服务器:.net核心API集成测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆