首页 > 解决方案 > 空手道比赛包含深度无法正常工作

问题描述

我们来自这里:https ://github.com/intuit/karate#match-contains-deep

正如它所说:

这会修改 match contains 的行为,以便为“深度包含”匹配处理嵌套列表或对象,...,您只想检查各种“树”数据中的某些值

所以让我们试着玩一下,找到一些{e: 5},它会在树的深处,当original = { a: 1, b: 2, c: 3, d: { a: 1, b: 2, e: 5 } }

Feature: Match contains deep

 # match contains deep test (works ok, but this is not "deep" by any stretch of imagination, this is guiding the whole path in the search)
 Scenario: match contains deep test modified original
   * def original = { a: 1, b: 2, c: 3, d: { a: 1, b: 2, e: 5 } }
   * def expected = { d: { e: 5 } }
   * match original contains deep expected

 #Thats was not deep, this is deep (fails, and this is what anyone would understand by "deep")
 Scenario: match contains deep
   * def original = { a: 1, b: 2, c: 3, d: { a: 1, b: 2, e: 5 } }
   * def expected = { e: 5 }
   * match original contains deep expected

 #So you dont need deep (fails)
 Scenario: match contains test modified original without deep
   * def original = { a: 1, b: 2, c: 3, d: { a: 1, b: 2, e: 5 } }
   * def expected = { d: { e: 5 } }
   * match original contains expected

 #So maybe it works with any (fails)
 Scenario: match contains test modified original
   * def original = { a: 1, b: 2, c: 3, d: { a: 1, b: 2, e: 5 } }
   * def expected = { e: 5 }
   * match original contains any expected

 #Maybe I'm tripping with syntax (fails)
 Scenario: match contains deep test my test #2
   * def original = { a: 1, b: 2, c: 3, d: { a: 1, b: 2, e: 5 } }
   * def expected = e: 5
   * match original contains deep expected

 #So maybe I'm tripping with syntax and semantics, and its any (fails)
 Scenario: match contains deep test my test #2
   * def original = { a: 1, b: 2, c: 3, d: { a: 1, b: 2, e: 5 } }
   * def expected = e: 5
   * match original contains any expected
 

所以,要么我没有真正得到这个东西,要么它不能像预期的那样工作,即我想检查放置在树中任何位置的现有键值对。

如果有人可以对此有所启发,那就太好了。正如我所看到的,@PeterThomas 正在回答大多数用空手道标记的问题,我要感谢他为将这个工具交到社区手中所做的巨大努力。

标签: karate

解决方案


@Greco,对不起,迟到了你是对的,最初我还认为 deep 能够在任何地方找到键值对,但遗憾的是它不是这样的。我理解这些术语的方式是:

contains - 在根对象中找到提到的键,但它们的值完全匹配。contains deep - 在根对象中找到提到的键,然后可以允许这些键对其值进行部分匹配,就像 contains 允许根对象进行部分匹配一样。

就像@Peter 说的那样,你必须形成一条路径,但它可以达到任何水平。

* def original = { a: 1, b: 2, c: 3, d: { a: 1, b: 2, e: {f:5,g:6} } }
* def expected = { d: { e: {f:5} } }
* match original contains deep expected

推荐阅读