cucumber - 如何使用 Karate JS 进行精确的复杂 JSON API 响应匹配
问题描述
假设这是我的 API 响应:
{
"name": "hello-world",
"listObjects": [
{
"id": 100,
},
{
"id": 200,
}
]
}
我希望通过以下响应来验证这一点:
{
"name": "hello-world",
"listObjects": [
{
"id": 100,
},
{
"id": 200,
}
]
}
为此,我正在做:response == myJson
。这完美!
但listObjects
可以按任何顺序排列。有时响应可能是这样的:
"name": "hello-world",
"listObjects": [
{
"id": 200,
},
{
"id": 100,
}
]
}
在这种情况下,如何仅在一行中进行精确的 json 匹配?我不想进行单独的密钥匹配。它应该在一行中完成。
解决方案
您至少需要 2 行。阅读文档以了解更多信息:
* def list = [{ id: 200 }, { id: 100 }]
* def response =
"""
{
"name": "hello-world",
"listObjects": [
{
"id": 100,
},
{
"id": 200,
}
]
}
"""
* match response.listObjects contains only list
* match response == { name: 'hello-world', listObjects: '#(^^list)' }
推荐阅读
- oracle - 在项目上分配数量
- javascript - 如何从散点图中选择一个点并显示有关它的数据?
- mysql - 防止 INSERT INTO ... SELECT 语句创建自己的新快照
- angular - 简短的 TypeScript 导入语句语法在 Angular 中不起作用
- angular - cropper.replace 不替换图像
- windows - 使用 PowerShell 远程编辑注册表项的问题
- python - LSTM 的时间序列分类中的损失和准确率保持不变
- apache - 如何将子域重定向到根域?
- odata - 如何在 SET_GET_ENTITY 方法中为“数量”类型的字段编写 SELECT?
- sql - 是否可以在 WHERE 子句中使用 IF 语句并根据不同的 IF 值使用 INTO 语句?