karate - 如何在许多对象中断言 2 个键
问题描述
回应 =
[
{
id: "1",
modelId: "301",
modelName: "x",
trimId: "301",
trimName: "up!",
derivativeName: "xx",
fuelType: "PETROL",
tco: {
price: {
tco: null,
retail: 12,
offer: null,
subsidy: null,
residual: null
},
milesPerYear: 10000,
mpg: 51.5,
fuelCost: 1.1,
milesPerKW: 0,
monthsOfOwnership: 48,
energyCost: 12,
tax: 42,
comparable: false
}
},
{
id: "1239",
modelId: "301",
modelName: "up!",
trimId: "301",
trimName: "x",
derivativeName: "xx",
fuelType: "PETROL",
tco: {
price: {
tco: null,
retail: 1,
offer: null,
subsidy: null,
residual: null
},
milesPerYear: 10000,
mpg: 53.2,
fuelCost: 1.1,
milesPerKW: 0,
monthsOfOwnership: 48,
energyCost: 12,
tax: 4,
comparable: false
}
}
]
一直在努力解决这个问题,但无法找出最好的方法。
我正在尝试检查何时 key fuelType
= PETROL 然后 keyfuelCost
必须等于 1.1
响应中有很多对象,我想在每个对象中声明 2 个键(fuelType 和 Fuelcost)。
我尝试了以下方法:
Given path 'url'
When method GET
Then match each response[*] contains {[*].fuelType:PETROL , [*].tco.fuelCost:1.1}
请帮忙谢谢
解决方案
试试这个,如果需要调整:
* def isValid = function(x){ return if x.fuelType == 'PETROL' && x.fuelCost == 1.1 }
* match each response == '#? isValid(_)'
推荐阅读
- python - 我的 while 循环中的变量没有被更新
- java - 在代码下找不到任何消息...对于语言环境 en_US
- salesforce - VSCode 没有推送代码,也没有抛出任何错误
- python - 使用python从API下载
- android - 如何在 Android 中的其他布局上显示半透明图像?
- python - Python中使用优先级队列的LRU缓存实现
- c++ - * 标记之前的预期标识符
- html - ul /li (list) 在 vue js 自动完成组件中不刷新或更新?
- json - jq - 根据两个 JSON 文件的键值的差异创建一个新对象
- python - 查询 API 返回意外响应