如何针对JSON数组中的嵌套对象断言“匹配包含”?

问题描述

如何针对JSON数组中的嵌套对象断言匹配包含

 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',age: 25 }
        ]
      }
      """
      * def expected = [{ id: 42,name: 'Bob' }]
    Then match cat.kittens contains '#(^expected)'

这个很好用,但是对我没有帮助。

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

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

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)