空手道框架-如何检查条件或匹配中包含响应 [英] Karate framework - How to check conditional OR in match contains response
问题描述
作为示例,我试图使用匹配包含来验证我的架构响应和数据类型,有时返回null,有时返回字符串.我正在尝试以下操作,但由于断言的评估结果不正确,因此断言失败.
I am trying to use a match contains to verify my schema response and data types and sometimes it returns a null and sometimes it will return a string, as an example. I am trying the following but I am getting the assertion failed because it did not evaluate to true.
我正在尝试以下操作:
* match each $response.data.Results contains
"""
{
"providerID": '#number',
"firstName": "#? _ == '#string' || _ == '#null'",
"lastName": '#string',
"mI": "#? _ == '#string' || _ == '#null'",
"title": '#string',
"name": '#string',
"nameLFMT": '#string',
"status": '#string',
"specialties": '#array',
"locations": '#array',
"institutions": '#array',
"acceptNewPatient": '#string',
"imageUri": '#string',
"nearestLatitude": '#number',
"nearestLongitude": '#number'
}
"""
例如,为"firstName"返回的数据为"firstName":空,
The data returned for "firstName" for example is "firstName":null,
在比赛之前,我每次发送此邮件:
Prior to the match each I am sending this:
Scenario: SearchResults
#Verify 200 response status returned
Given text query =
"""
{
Results: getSearchResults(searchLatitude:"48.942833",
searchLongitude: "-119.984549",
providerType: "Primary Care Physicians",
sortBy: "distance",
maxDistance:"600",
skip: 0,
take: 10) {
providerID
firstName
lastName
mI
title
name
nameLFMT
status
specialties
locations
institutions
acceptNewPatient
imageUri
nearestLatitude
nearestLongitude
}
}
"""
And request { query: '#(query)' }
When method post
Then status 200
我没有定义架构,我还没有弄清楚如何做到这一点,所以我不确定这是否是我的问题.我知道这可能是我应该怎么做,但我仍在学习.
I am not defining the schema, I have yet to figure out how to do this so I am not sure if that is my issue. I know this is probably how I should do this but I'm still learning.
感谢您的帮助.
推荐答案
好,我在这里看到一个问题:
Ok I see one problem here:
"firstName": "#? _ == '#string' || _ == '#null'"
那是行不通的. #?
之后的部分必须是有效的JavaScript表达式.如果您不熟悉JS,但#string
和朋友是空手道match
关键字和 not 特定于JavaScript的东西的朋友,这可能很难理解.
That's not going to work. The part that follows the #?
has to be a valid JavaScript expression. This may be hard to understand if you are not familiar with JS but #string
and friends are specific to the Karate match
keyword and not something that works in JavaScript.
因此,以上一行所做的是检查firstName
的 value 是否等于文字字符串#string
.
So what the above line is doing is checking if the value of firstName
is equal to the literal string #string
.
幸运的是,空手道有一个解决方案:(请参考文档"模糊匹配) :
Fortunately Karate has a solution: (refer doc for 'fuzzy matching'):
"firstName": "##string"
这很方便,双精度##
表示可选",它将与两者字符串或null
匹配.
Which is a convenience, the double ##
means 'optional' that will match for both a string or null
.
对于高级情况,请阅读以下内容: https://stackoverflow.com/a/50350442/143475
for advanced cases, please read this: https://stackoverflow.com/a/50350442/143475
这篇关于空手道框架-如何检查条件或匹配中包含响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!