【问题标题】:Karate: json path element equals not working空手道:json 路径元素等于不工作
【发布时间】:2020-07-24 11:24:43
【问题描述】:

我有这个功能:

Given path 'group/'
When method get
Then status 200
And match response.group[*].name contains [ 'group1' ] #Works
And match response.group[?(@.name == 'group1')].name contains [ 'group1' ] #Does not work

这是group/端点的响应:

{
  "group": [
    {
      "name": "group1",
      "id": 1
    },
    {
      "name": "group2",
      "id": 2
    }
  ]
}

我收到此错误:

groups.groups: groups.feature:35 - evaluation (js) failed: 'group1')].name contains [ 'group1' ], <eval>:1:8 Expected ; but found )
'group1')].name contains [ 'group1' ]
        ^ in <eval> at line number 1 at column number 8

我检查了https://github.com/json-path/JsonPath#path-examples 中的语法,似乎没问题。

提前致谢。

已编辑:非工作表达式只是保持问题简单的一个示例。实际上,我想从列表中获取整个组,这样我就可以比较 id 等其他字段,并使用我无法与 contains 一起使用的模糊匹配器:

And match response.group[?(@.name == 'group1')][0] ==
"""
{
  "name": "group1",
  "id": #notnull
}
"""

【问题讨论】:

    标签: karate


    【解决方案1】:

    是的,这是解析器的错误,LHS 上的括号会混淆它。但这很少见(在这种情况下是超级做作的)。做两步:

    * def temp = $.group[?(@.name == 'group1')].name
    * match temp contains [ 'group1' ]
    

    请随时针对文档或代码提交 PR,谢谢 ;)

    【讨论】:

    • 我编辑了问题以显示我真正的问题(答案仍然有效)。我试图检索列表的一个元素,以便可以使用模糊匹配器,因为包含运算符不支持它们。解决方法很好(注意:如果我使用 'response' 而不是 '$' 表达式将停止工作)。
    • @italktothewind 注意:阅读文档github.com/intuit/karate#get-short-cut
    • 我明白了,'response' 仅在与匹配运算符一起使用时才有效。
    • @italktothewind 不。如果它是 JS 而不是 JsonPath,则响应将起作用,例如response.foo[0] 会工作
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-27
    相关资源
    最近更新 更多