gpt4 book ai didi

api - 断言和使用 Karate 中数组响应的条件

转载 作者:行者123 更新时间:2023-12-04 09:36:22 24 4
gpt4 key购买 nike

我有一个请求,它根据“状态”以两种可能的结构返回响应列表。

{
"listSize": 2,
"itemList": [
{
"id": ,
"Name": "",
"submittedOn": "",
"Reference": null,
"status": "Receipted",
"response": null
},
{
"id": 12345,
"submittedOn": "",
"Reference": null,
"status": "Failed",
"response": {
"xml": "",
"formErrors": [
{
"error_type": "",
"error_location":"",
"error_message": "",
}
]
}
},
]
}

我需要检查状态为“已接收”或“失败”的结构。在 Java 中,我将使用 for 循环和其中的 if 语句根据“状态”字段检查具有不同标准的响应字段。 (下例)
for (int i = 0; i < response.length; i++){
if (response[i].status.equals("receipted")){
//do something
}
else{ //failed
//do something else
}
}
我怎样才能在空手道中取得类似的成绩?我应该使用 Java Helper 吗?

最佳答案

首先,鼓励您在测试中编写静态预期结果。也就是说有多种方法可以做到这一点,这里是一种:

* def failedSchema = { xml: '#string', formErrors: '#array' }
* def isValid = function(x){ if (x.status == 'Receipted') return x.response == null; return karate.match(x.response, failedSchema).pass }
* match each response.itemList == '#? isValid(_)'
这是另一个例子: https://stackoverflow.com/a/62567412/143475
在空手道中还有其他循环方式,但并非真正为匹配而设计: https://github.com/intuit/karate#loops
这是一个涉及 JSON 转换以使其更易于匹配的极端示例: https://stackoverflow.com/a/53120851/143475
另请参阅: https://github.com/intuit/karate#conditional-logic

关于api - 断言和使用 Karate 中数组响应的条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62566219/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com