问题描述
我正在尝试使用匹配包含来验证我的架构响应和数据类型,例如,有时它返回一个空值,有时它会返回一个字符串.我正在尝试以下操作,但我的断言失败了,因为它没有评估为真.
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":null,
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"
这很方便,双 ##
表示可选",它将匹配 both 字符串或 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
这篇关于空手道框架 - 如何检查匹配中的条件 OR 包含响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!