首页 > 解决方案 > 空手道:包含功能

问题描述

我有以下内容,我正在比较两个环境的响应。在我的情况下,对于某些服务,响应 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.[*] 

标签: karate

解决方案


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)]]

推荐阅读