在场景中使用匹配的正确语法

Proper syntax for using match in Scenario

我将空手道用于我的模拟服务,因此我的空手道文件具有包含我需要与 运行 该场景匹配的参数的场景。我在以下方面取得了成功: pathMatches('my/api/path') && methodIs('post') && (karate.match("json.array[*].key contains null").pass

我现在想根据我在本文中找到的内容添加类似 'or json.array[*].key == #isnotpresent' 的内容:https://github.com/intuit/karate/issues/270

看起来你在测试正文中使用了 match 关键字,但我怎么能在场景中这样做呢?使用 karate.match()?如果是这样,正确的语法是什么?我知道我需要更多地做这类事情,所以我想掌握它。

我试过类似的方法:karate.match('json.array[*] contains { key: 'notpresent'}').pass karate.match('json.array[*] == {key: '#notpresent'}').pass 没有成功。

我正在使用 0.9.6 版的空手道,我在问这个问题时发现:

谢谢!

这里有两个提示:

a) 在正常的空手道测试中测试你的 Scenario 表达式,如下所示:

* def temp = 
"""
{
    "Array": [

        {
            "Id": "legitId"
        },
        {
            "foo": "bar"
        }
    ],
}
"""
#  
* assert karate.match("temp.Array[*].Id contains null").pass || !karate.match("each temp.Array contains { Id: '#present' }").pass

b) 您可以在 Background 中定义函数并在 Scenario 表达式中使用:

Background:
* def isIdMissing = function(){ return karate.match("request.Array[*].Id contains null").pass || !karate.match("each request.Array contains { Id: '#present' }").pass }

Scenario: pathMatches('my/api/path') && methodIs('post') && isIdMissing()