【问题标题】:Trying to assert 2 dynamic responses试图断言 2 个动态响应
【发布时间】:2019-07-17 20:47:40
【问题描述】:

我有两种不同的回答:

{
      "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 }

因此,如果键 price 在任何响应中以“.5”结尾,则应该是通过。如果关键 priceall 响应中的整数,那么它应该会失败。

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

【问题讨论】:

    标签: karate


    【解决方案1】:

    有几种方法。另请阅读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]+'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-14
      • 1970-01-01
      • 1970-01-01
      • 2019-01-12
      • 1970-01-01
      • 2017-06-11
      • 2017-07-30
      • 2021-02-24
      相关资源
      最近更新 更多