空手道断言的动态响应

时间:2020-05-30 11:35:56

标签: karate

实际响应:

{
"data": [
    {
      "last_name": "Bluth",
      "id": 1,
      "avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/calebogden/128.jpg",
      "first_name": "George",
      "email": "george.bluth@reqres.in"
    },
    {
      "last_name": "Weaver",
      "id": 2,
      "avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/josephstein/128.jpg",
      "first_name": "Janet",
      "email": "janet.weaver@reqres.in"
    }
  ]
}

我有一个Database对象,它返回first_name的数组

预期:

{"George","Janet"}

我在这里使用方案概述,因为我需要检查大量测试,并且输出是动态的。我们如何为这种情况编写断言? 类似于response.data []。first_name ==预期??

1 个答案:

答案 0 :(得分:2)

示例代码:

Feature: Array Match

Scenario: 
    * def act = 
    """
{
  "per_page": 6,
  "total": 12,
  "ad": {
    "company": "StatusCode Weekly",
    "text": "A weekly newsletter focusing on software development, infrastructure, the server, performance, and the stack end of things.",
    "url": "http://statuscode.org/"
  },
  "data": [
    {
      "last_name": "Bluth",
      "id": 1,
      "avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/calebogden/128.jpg",
      "first_name": "George",
      "email": "george.bluth@reqres.in"
    },
    {
      "last_name": "Weaver",
      "id": 2,
      "avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/josephstein/128.jpg",
      "first_name": "Janet",
      "email": "janet.weaver@reqres.in"
    },
    {
      "last_name": "Wong",
      "id": 3,
      "avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/olegpogodaev/128.jpg",
      "first_name": "Emma",
      "email": "emma.wong@reqres.in"
    },
    {
      "last_name": "Holt",
      "id": 4,
      "avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/marcoramires/128.jpg",
      "first_name": "Eve",
      "email": "eve.holt@reqres.in"
    },
    {
      "last_name": "Morris",
      "id": 5,
      "avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/stephenmoon/128.jpg",
      "first_name": "Charles",
      "email": "charles.morris@reqres.in"
    },
    {
      "last_name": "Ramos",
      "id": 6,
      "avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/bigmancho/128.jpg",
      "first_name": "Tracey",
      "email": "tracey.ramos@reqres.in"
    }
  ],
  "page": 1,
  "total_pages": 2
}
    """
    * def exp = ["George","Janet","Emma","Eve","Charles","Tracey"]
    * match $act.data[*].first_name == exp

参考链接:https://github.com/intuit/karate#get-short-cut