首页 > 解决方案 > 空手道模式验证,在使用匹配包含断言时它是否适用于嵌套数组?

问题描述

我试图通过仅检查某些元素来对 json 对象使用匹配项。Json 对象包含一个嵌套数组,这似乎是测试失败的根本原因。似乎在嵌套数组中的 key:values 上使用“匹配包含”断言无法按预期工作并引发错误。

我尝试在第一级键上使用“匹配包含”并且效果很好,但是当尝试匹配嵌套数组中的键时,我收到以下错误消息:原因:实际值比预期多 1 个键

* def json = { id: 1, priority: 1, compRound: { id: 1, comp_id: 89 } }
* match json contains
"""
{
  id: '#number',
  priority: '#number',
  compRound: {
    id: '#number'
  }
}
"""

当我使用match contains断言时,我希望测试能够通过,但看起来空手道希望嵌套数组中的所有键:值都存在。

标签: karate

解决方案


请仔细阅读文档的这一部分:https ://github.com/intuit/karate#contains-short-cuts

* def json = { id: 1, priority: 1, compRound: { id: 1, comp_id: 89 } }
* def compRound = { id: '#number' }
* match json == { id: '#number', priority: '#number', compRound: '#(^compRound)' }

推荐阅读