模式验证-空手道表达式以检查数组中是否存在值

时间:2019-03-11 13:42:08

标签: karate

示例响应

{
    "data": [
    {
        "name": "DJ", 
        "status": "ACTIVE"
    } 
    ]
}

示例功能文件

@ignore
Feature: Sample

  @smoke
  Scenario: Karate expression to check if value exists in array 
    Given url url
    And path '/test'
    When method GET
    Then status 200
    And def users = response.data
    And def possibleStatus = ["ACTIVE", "INACTIVE"]

    And def schema =
    """
    {
      name: '#string',
      status: ? 
    }
    """
    And match each users contains schema

是否有一种方法可以使用空手道表达式检查状态为“活动”还是“不活动”?

注意:可以通过编写自定义JS函数来实现。

1 个答案:

答案 0 :(得分:1)

* def statuses = [ 'ACTIVE', 'INACTIVE' ]
* def response = [{ name: 'DJ', status: 'ACTIVE' }, { name: 'PJ', status: 'INACTIVE' }]
* match each response == { name: '#string', status: '#? statuses.contains(_)' }