试图断言2个动态响应

时间:2019-07-17 09:31:20

标签: karate

我有2种不同的回答:

{
      "id": "U204204",
      "title": "Safety kit",
      "categoryPath": "/equipment/accessories/null",
      "keyFeature": false,
      "description": "test",
      "price": 24.5,
      "availability": "optional-extra",
      "technologyItems": [],
      "bespoke": false
    }

或/和

{
      "id": "GWW1WW1",
      "title": "Winter pack",
      "categoryPath": "/comfort & convenience/packs/null",
      "keyFeature": false,
      "description": "test",
      "price": 410,
      "availability": "optional-extra",
      "technologyItems": [],
      "bespoke": false
    }

现在我要声明的是,只要上述2个响应中任何一个的键 price 具有以'.5'结尾的值,就可以通过它。

我尝试了以下和类似的方法,但是没有用:

Given path 'endpoint'
    And multipart file pdbData =  { read: 'json/PostRequest_201_3.json', filename: 'PostRequest_201_3.json', contentType: 'application/json'}
    When method post
    And status 201
    * def NewpdbId = response.id
    And path 'endpoint'+NewpdbId+'/V3FOY3DG'
    And method GET
    And status 200
    * string aa = response.options[13].price
    * string bb = response.options[16].price

    * def expected = aa contains "#redgex .+[.5]+" ? { pass: true  } 
    * def expected = bb contains "#regex .+[.5]+" ? { pass: true } 

    * string expected = expected


    And match expected == { pass: true }

因此,如果键价格在任何响应中均以“ .5”结尾,则应为通过。如果键 price 是响应的全部中的整数,则它将失败。

有什么想法吗?我尝试了很多不同的方式

1 个答案:

答案 0 :(得分:2)

有几种方法。另请阅读match each的文档:

首先仅将price值提取到列表中,转换为字符串(请阅读JSON transforms的文档):

* def prices = $response.options[*].price
* def fun = function(x){ return x + '' }
* def prices = karate.map(prices, fun)
* match prices contains '#regex .+[.5]+'