空手道框架 - 如何检查匹配中的条件或包含响应

Karate framework - How to check conditional OR in match contains response

我正在尝试使用匹配包含来验证我的模式响应和数据类型,有时它 return 是一个空值,有时它会 return 一个字符串,例如。我正在尝试以下操作,但断言失败,因为它未评估为真。

我正在尝试以下操作:

* 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" 的 return 数据是 "firstName":null,

每次比赛前我都会发送这个:

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

我没有定义模式,我还没有弄清楚如何做到这一点,所以我不确定这是否是我的问题。我知道这可能是我应该做的,但我仍在学习。

感谢您的帮助。

好的,我在这里看到一个问题:

"firstName": "#? _ == '#string' || _ == '#null'"

那是行不通的。 #? 之后的部分必须是有效的 JavaScript 表达式。如果你不熟悉 JS,这可能很难理解,但是 #string 和朋友们特定于空手道 match 关键字和 not 在 JavaScript.

所以上面一行所做的是检查 firstNamevalue 是否等于文字字符串 #string.

幸运的是空手道有一个解决方案:(参考文档'fuzzy matching'):

"firstName": "##string"

这很方便,double ## 表示 'optional' 将匹配 both 字符串或 null.

编辑:对于高级案例,请阅读: