karate - 空手道:包含功能
问题描述
我有以下内容,我正在比较两个环境的响应。在我的情况下,对于某些服务,响应 json 元素/数组顺序不是强制性的,但对于某些服务是强制性的。所以我使用了包含,但我怀疑在比较以下两个时是否有任何响应中缺少任何元素/数组:
A: {
"hotels": [
{ "roomInformation": [{ "roomPrice": 618.4 }], "totalPrice": 618.4 },
{ "roomInformation": [{ "roomPrice": 679.79}], "totalPrice": 679.79 }
]
}
B:
{
"hotels": [
{ "roomInformation": [{ "roomPrice": 618.4 }], "totalPrice": 618.4 },
{ "roomInformation": [{ "roomPrice": 680.79}], "totalPrice": 680.79 },
{ "roomInformation": [{ "roomPrice": 679.79}], "totalPrice": 679.79 }
]
}
使用下面的匹配每个都contains
给出积极的结果?请建议
Feature:
Background:
Scenario: test
* json input = read('input.json')
* def stage= call read('A.feature') input;
* def prod = call read('B.feature') input;
#* def rp = $prod[*].response
#* def rs = $stage[*].response
#* match rs contains rp
#* match each $prod[*].response[*] contains $stage[*].response
# * match each $prod[*].response[*] contains $stage[*].response.[*]
解决方案
Since order is important and it is in a array/list, a "equals" check than "contains" works as desired:
The following code results in an error.
Sample Code:
Feature: Validation
Scenario:
* def stage =
"""
{
"hotels": [
{ "roomInformation": [{ "roomPrice": 618.4 }], "totalPrice": 618.4 },
{ "roomInformation": [{ "roomPrice": 679.79}], "totalPrice": 679.79 }
]
}
"""
* def prod =
"""
{
"hotels": [
{ "roomInformation": [{ "roomPrice": 679.79}], "totalPrice": 679.79 },
{ "roomInformation": [{ "roomPrice": 618.4 }], "totalPrice": 618.4 }
]
}
"""
* match prod == stage
#ERROR: path: $.hotels[0], actual: {roomInformation=[{"roomPrice":679.79}], totalPrice=679.79}, expected: {roomInformation=[{"roomPrice":618.4}], totalPrice=618.4}, reason: [path: $.hotels[0].roomInformation[0], actual: {roomPrice=679.79}, expected: {roomPrice=618.4}, reason: [path: $.hotels[0].roomInformation[0].roomPrice, actual: 679.79, expected: 618.4, reason: not equal (Double)]]
推荐阅读
- lambda - 如果已经给出括号,则 lambda 演算中括号的顺序?
- react-native - 无效的挂钩调用。钩子只能在函数组件的主体内部调用。在反应原生签名画布中
- node.js - 在 Nodejs 中等待函数完成
- html - 我的下拉导航栏不起作用
- python - 使用 Qt 设计器布局调整图像大小
- r - 错误:包“CompQuadForm”的编译失败
- c# - 将 excel 解析为数据表并使用 NPOI 对每个单元格应用验证?
- android - 在 Jetpack Compose 中单击时如何禁用涟漪效应
- reactjs - 在 Angular 8+ 中使用 React 组件的最佳方法?
- flutter - Flutter:ModalBottomSheet 与 ListView.separated() 大小到内容