在场景中使用匹配的正确语法 [英] Proper syntax for using match in Scenario
问题描述
我将空手道用于我的模拟服务,所以我的空手道文件具有一些方案,其中包括运行该方案所需的参数.我在以下方面取得了成功:pathMatches('my/api/path')&&methodIs('post')&&(karate.match("json.array [*].key包含null"").pass
I'm using Karate for my mock service, so my karate files have Scenarios that include the params I need to match to run that scenario. I've had success with things like: pathMatches('my/api/path') && methodIs('post') && (karate.match("json.array[*].key contains null").pass
基于我在本文中发现的内容,我现在想添加类似或json.array [*].key == #isnotpresent"的内容: https://github.com/intuit/karate/issues/270
I now want to add something like 'or json.array[*].key == #isnotpresent' based on what I found in this article: https://github.com/intuit/karate/issues/270
您似乎在测试的正文中使用了match关键字,但是在方案中我该怎么做呢?使用karate.match()?如果是这样,正确的语法是什么?我知道我需要做更多这类事情,所以我想了解一下.
It looks like you use the match keyword in the body of a test, but how could I do this in the Scenario? Use karate.match()? If so, what's the proper syntax? I know I'll need to do this kind of stuff more, so I want to get a handle on it.
我尝试过类似的操作: karate.match('json.array [*]包含{键:'notpresent'}').pass
和 karate.match('json.array [*] == {key:'#notpresent'}').pass
没有成功.
I tried something like: karate.match('json.array[*] contains { key: 'notpresent'}').pass
and karate.match('json.array[*] == {key: '#notpresent'}').pass
without success.
I'm using 0.9.6 version of Karate that I found when I asked this question: Matching key value pairs in a request in a Karate Netty scenario
谢谢!
推荐答案
这里有两个提示:
a)在常规空手道测试中测试您的 Scenario
表达式,如下所示:
a) Test your Scenario
expressions in a normal Karate test like this:
* 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
表达式中使用:
b) You can define functions in the Background
and use in Scenario
expressions:
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()
这篇关于在场景中使用匹配的正确语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!