如何使用 C# 在 json 响应中遍历多个具有相同名称的子节点并声明正确的内容
How to iterate through multiple child nodes with same name in a json response using C# and assert right content
我收到的网络服务响应低于此值。
我想使用 specflow 和 C# Nunit 框架为此编写一个测试
{
"status": "Healthy",
"results": [
{
"name": "Microservice one",
"status": "Healthy",
"description": "Url check MSOneURI success : status(OK)"
},
{
"name": "Microservice two",
"status": "Healthy",
"description": "Url check MSTwoURI success : status(OK)"
},
{
"name": "Microservice three",
"status": "Healthy",
"description": "Url check MSThreeURI success : status(OK)"
},
{
"name": "Microservice four",
"status": "Healthy",
"description": "Url check MSFourURI success : status(OK)"
},
{
"name": "Microservice five",
"status": "Healthy",
"description": "Url check MSFiveURI success : status(OK)"
}
]
}
这是我的功能文件的样子
@uService
Feature: Micro Service - health check
In order to perform health check
As a service
I want to ensure downstream Micro Services are healthy
Scenario Outline: [uService] Status of downstream microservices are healthy
Given health check micro service
When health check is performed
Then <nameoftheservice> service returns correct description and status
Examples:
| downstreamservice |
| Microservice one |
| Microservice two |
| Microservice three |
| Microservice four |
| Microservice five |
我需要帮助编写 Then 步骤的绑定方法
您可以使用 Newtonsoft.JSON 包反序列化 JSON,然后使用 LINQ 遍历服务列表以进行比较。
类似于:
class MicroserviceStatus {
public string Name { get; set; }
public string Status { get; set; }
public string Description { get; set; }
}
class MicroserviceHealthCheck {
public string Status { get; set; }
public List<MicroserviceStatus> MicroserviceStatuses { get; set; }
}
var microserviceHealthCheck = JsonConvert.DeserializeObject<MicroserviceHealthCheck>(json);
bool anyNotHealthy = microserviceHealthCheck.MicroserviceStatuses.Any(i => i.Status != "Healthy");
我直接将其写入编辑器,因此它可能不完全正确,您应该已经拥有可用于反序列化的类型,但希望这会有所帮助。
我收到的网络服务响应低于此值。 我想使用 specflow 和 C# Nunit 框架为此编写一个测试
{
"status": "Healthy",
"results": [
{
"name": "Microservice one",
"status": "Healthy",
"description": "Url check MSOneURI success : status(OK)"
},
{
"name": "Microservice two",
"status": "Healthy",
"description": "Url check MSTwoURI success : status(OK)"
},
{
"name": "Microservice three",
"status": "Healthy",
"description": "Url check MSThreeURI success : status(OK)"
},
{
"name": "Microservice four",
"status": "Healthy",
"description": "Url check MSFourURI success : status(OK)"
},
{
"name": "Microservice five",
"status": "Healthy",
"description": "Url check MSFiveURI success : status(OK)"
}
]
}
这是我的功能文件的样子
@uService
Feature: Micro Service - health check
In order to perform health check
As a service
I want to ensure downstream Micro Services are healthy
Scenario Outline: [uService] Status of downstream microservices are healthy
Given health check micro service
When health check is performed
Then <nameoftheservice> service returns correct description and status
Examples:
| downstreamservice |
| Microservice one |
| Microservice two |
| Microservice three |
| Microservice four |
| Microservice five |
我需要帮助编写 Then 步骤的绑定方法
您可以使用 Newtonsoft.JSON 包反序列化 JSON,然后使用 LINQ 遍历服务列表以进行比较。
类似于:
class MicroserviceStatus {
public string Name { get; set; }
public string Status { get; set; }
public string Description { get; set; }
}
class MicroserviceHealthCheck {
public string Status { get; set; }
public List<MicroserviceStatus> MicroserviceStatuses { get; set; }
}
var microserviceHealthCheck = JsonConvert.DeserializeObject<MicroserviceHealthCheck>(json);
bool anyNotHealthy = microserviceHealthCheck.MicroserviceStatuses.Any(i => i.Status != "Healthy");
我直接将其写入编辑器,因此它可能不完全正确,您应该已经拥有可用于反序列化的类型,但希望这会有所帮助。