【问题标题】:Are #ignore and #present the same thing?#ignore 和#present 是一回事吗?
【发布时间】:2018-12-20 15:53:01
【问题描述】:

在什么情况下我想使用 #ignore 与 #present 或反之亦然?还是它们是一样的?

我第一次阅读文档时认为以下内容会通过,但事实并非如此。 编辑添加:这在空手道 0.9.0 中失败,但在 0.8.0 中通过。

* def foo = {a: 1}
* match foo == {a: 1, b: "#ignore"}

这些都通过了:

* def foo = {a: 1}
* match foo == {a: 1, b: "##ignore"}
* match foo == {a: 1, b: "##present"}

【问题讨论】:

    标签: karate


    【解决方案1】:

    是的,当您想匹配不存在或为空的键时,请使用双哈希:

    * def foo = { a: 1 }
    * match foo == { a: 1, b: '##ignore' }
    * match foo == { a: 1, b: '#notpresent' }
    
    * def foo = { a: 1, b: null }
    * match foo == { a: 1, b: '##ignore' }
    * match foo == { a: 1, b: '#present' }
    
    * def foo = { a: 1, b: 'bar' }
    * match foo == { a: 1, b: '##ignore' }
    * match foo == { a: 1, b: '#present' }
    

    【讨论】:

    • 谢谢@Peter。您同意编辑 #ignore 文档吗? #present 文档很清楚。
    • @AlexJ 感谢您展示这些边缘案例,非常感谢:github.com/intuit/karate/issues/628
    【解决方案2】:

    不,#ignore 和 #present 的行为并不相同。

    Karate 0.9.0 中有一个错误导致它们的行为相同,但那是 fixed。

    #ignore 应该匹配密钥是否存在。

    # This case would NOT match with '#present'
    # This case fails in Karate 0.9.0 due to a bug
    * def foo = {}
    * match foo == { a: '#ignore' }
    
    # These cases would also match with '#present'
    * def foo = {a: null}
    * match foo == { a: '#ignore' }
    * def foo = {a: "bar"}
    * match foo == { a: '#ignore' }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-21
      • 2013-05-01
      • 2011-06-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多