【问题标题】:Karate: verify text contains in a list of kittens in cat return all key-values did not match error空手道:验证文本是否包含在 cat 中的小猫列表中,返回所有键值不匹配错误
【发布时间】:2020-02-21 01:56:43
【问题描述】:

我有以下代码

* def cat =
  """
  {
    kittens: [
      {
        id: 23,
        name: 'Bob'
      },
      {
        id: 42,
        name: 'Wild'
      }
    ]
  }
  """
* def id = {id: 23}

我正在尝试关注Karate contains and all key-values did not match error 来解决所有键值不匹配错误 这就是我所拥有的

* match cat == {kittens: '#(^id)'}
* match cat contains {kittens: '#(^id)'}

但我收到以下错误

com.intuit.karate.exception.KarateException: - 路径: $, 实际: {kittens=[{"id":23,"name":"Bob"},{"id":42,"name" :"Wild"}]},预期:{kittens=#(^id)},原因:所有键值都不匹配

现在我为每只小猫添加了另一个颜色字段:

* def cat =
  """
  {
    kittens: [
      {
        id: 23,
        colour: 'black',
        name: 'Bob'
      },
      {
        id: 42,
        colour: 'grey',
        name: 'Wild'
      }
    ]
  }
  """
* def id = {id: 23, name: 'Bob'}

使用提供的答案cat.kittens contains '#(^id)' 仍然有效。

另一种方法是

* def id = {id: 23, colour: '#number', name: 'Bob'}

选项 1 更好,因为您不需要指定颜色:'#number'。

【问题讨论】:

    标签: karate


    【解决方案1】:

    给你:

    * match cat.kittens contains '#(^id)'
    

    如需解释,请阅读:https://github.com/intuit/karate#contains-short-cuts

    【讨论】:

    • 谢谢,彼得,这很好用。对于每只小猫,我添加了另一个字段颜色,位于 id 和 name 字段之间。现在我的 id 是 { id: '23', name: 'Bob' } 但它不起作用。我必须设置为{ id: '23', colour: '#number', name: 'Bob' } 才能让它工作。如果我在 kitten 中有很多字段但我​​只想验证 2-3 个字段怎么办。有没有办法在不指定 colour: '#number', 的情况下解决这个问题?
    • @sc30 请提出一个新问题
    • 我只能每 90 分钟发布一个新问题,稍后再发布。
    • @sc30 是的,因为在 cmets 中很难讨论。据我了解,我的答案本身应该有效,所以我迷路了
    • 嗨,彼得,我的代码中有错字,所以您的解决方案实际上仍然有效。再次感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 2020-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多