空手道中包含的 JSON 数组的深度断言

Deep assertion on JSON array with contains in Karate

 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' }]

如示例所示,我并不担心 'age'。我知道我可以使用“#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

编辑:

我尝试了 中的建议 但这对我没有帮助。我不是在寻找模式验证,而是在寻找功能验证,其中每个对象可能具有不同的键值。

下面一个失败

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

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

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

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

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