【问题标题】:Deep assertion on JSON array with contains in Karate空手道中包含 JSON 数组的深度断言
【发布时间】:2020-08-18 15:07:31
【问题描述】:
 Scenario: 
    Given def cat =
      """
      {
        name: 'Billie',
        kittens: [
          { id: 23, name: 'Bob', age: 35 },
          { id: 42, name: 'Wild', age: 25 }
        ]
      }
      """
    Then match cat.kittens contains [{ id: 42, name: 'Wild' }, { id: 23, name: 'Bob' }]

如示例所示,我并不担心“年龄”。我知道我可以使用“#ignore”。如果我有很多这样的节点我必须忽略,还有其他方法吗?

我只是在想,它能否像处理 JSON 对象一样处理 JSON 数组。只断言指定的节点。

错误:

assert.feature:24 - path: $.kittens[*], actual: [{"id":23,"name":"Bob","age":35},{"id":42,"name":"Wild","age":25}], expected: {id=42, name=Wild}, reason: actual value does not contain expected

编辑:

我尝试了Karate - how to check if array contains values? 中的建议 但这对我没有帮助。我不是在寻找模式验证,而是在寻找功能验证,其中每个对象可能具有不同的键值。

低于一个失败

  Scenario: 
    Given def cat =
      """
      {
        name: 'Billie',
        kittens: [
          { id: 23, name: 'Bob', age: 35 },
          { id: 42, name: 'Wild', age: 25 }
        ]
      }
      """
      * def expected = [{ id: 42, name: 'Wild' }, { id: 23, name: 'Bob' }]
    Then match cat.kittens contains '#(^expected)'

这个工作正常,但这对我没有帮助。

  Scenario: 
    Given def cat =
      """
      {
        name: 'Billie',
        kittens: [
          { id: 23, name: 'Bob', age: 35 },
          { id: 42, name: 'Wild', age: 25 }
        ]
      }
      """
      * def expected = { id: 42, name: 'Wild' }
    Then match cat.kittens contains '#(^expected)'

目前,我正在单独读取数组并使用循环声明它们。

【问题讨论】:

    标签: karate


    【解决方案1】:

    在 0.9.6.RC4 中只需添加单词 deep 即可

    Then match cat.kittens contains deep [{ id: 42, name: 'Wild' }, { id: 23, name: 'Bob' }]
    

    我不明白为什么我关闭您的第一个问题时链接的答案对您没有帮助。也许我不明白,其他人可以提供更好的答案。

    【讨论】:

    • 它就像一个 0.9.6 RC4 的冠军。但我不确定 Then match cat.kittens contains '#(^expected)' 在 0.9.5 中如何处理这种情况
    • @kishorereddy 我不明白你的问题。阅读文档:github.com/intuit/karate#schema-validation - 如果可能的话,我把它留给其他人,以便提供更好的答案
    猜你喜欢
    • 2019-05-22
    • 2022-11-23
    • 1970-01-01
    • 2017-12-29
    • 1970-01-01
    • 2020-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多