【问题标题】:Karate: contains functionality空手道:包含功能
【发布时间】:2020-02-05 05:39:40
【问题描述】:

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


    【解决方案1】:

    您的问题有些复杂,难以回答。

    此外,您的断言可以是预期整体的部分列表。使用print命令帮你调试。

    * def roomPrices = response.hotels[*].totalPrice
    * print roomPrices
    * match roomPrices contains [ 680.79, 1.00 ]
    

    【讨论】:

    • 在我的情况下,我比较 stage 与 prod 并动态比较整个响应,两个区域都有相同的数据,所以两个结果应该是相同的。对于某些服务,JSON 中响应元素的顺序会随机排列,因此当我执行精确的 match (==) 甚至包含时会失败。我的假设是contains 如果顺序不同,会给出积极的结果!
    • 就个人而言,我从不尝试比较整个响应。我通常会使用karate.filter 将响应内容缩小到我想要区分的区域。另外,我刚刚意识到我上面的示例代码可能无法正常工作。可能将assert 更改为match,因为DSL contains 操作符可能在match 操作中。我对这些东西也还是陌生的。 (编辑了我的答案)
    【解决方案2】:

    由于顺序很重要并且它位于数组/列表中,因此“等于”检查而不是“包含”可以根据需要进行检查:

    以下代码导致错误。

    示例代码:

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

    【讨论】:

      猜你喜欢
      • 2020-08-14
      • 1970-01-01
      • 1970-01-01
      • 2020-04-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多