[英]How to iterate through multiple child nodes with same name in a json response using C# and assert right content
我正在从Web服务获得以下响应。 我想使用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");
我直接将其写到编辑器中,因此它可能不完全正确,并且您应该已经具有可用于反序列化的类型,但是希望这会有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.