【问题标题】:Karate contains and all key-values did not match error空手道包含且所有键值不匹配错误
【发布时间】:2019-01-10 12:07:37
【问题描述】:

我尝试学习空手道,但遇到一些问题,我无法自己解决。

所以我的功能看起来很简单:

Feature: Alerting get the list of all alerts
  Background:
    * url 'url'

  Scenario: Retrieve all alerts

    Given path '5c348c553a892c000bb1f2dd'
    When method get
    Then status 200
    And match response contains {id: 5c348c553a892c000bb1f2dd}

这里的情况是获取响应并确保给定的 ID 在列表中。据我了解,这个 documentation 关键字包含应该只查找给定的短语,但我得到一个错误:原因:所有键值都不匹配

这是我的控制台输出:

allAlertsGet.feature:10 - path: $, actual: {data={name=Baelish of Harrenhal, user=griffin, id=5c348c553a892c000bb1f2dd, tags=["Gared"], triggers={prometheus=[{"js_id":"Qarth","labels":["Harry Potter and the Sorcerer's Stone"],"operator":"==","query":"up","value":"1"}]}, trigger_interval=398s, receivers={slack=[{"holdoffTime":"0s","id":"Stalls","message":"Dark and difficult times lie ahead. Soon we must all face the choice between what is right and what is easy.","revokeMessage":"Every flight begins with a fall.","token":"Buckbeak"}]}, hold_cap=4, max_cap=16, factor=2, createDate=1546947669, triggered_date=1546948867, mute_until=0, muted=false, status=3}}, expected: {id=5c348c553a892c000bb1f2dd}, reason: all key-values did not match

我错过了什么?我使用空手道 0.9.0。

【问题讨论】:

    标签: api karate


    【解决方案1】:

    注意 JSON 的嵌套结构。您可以将此 sn-p 粘贴到 Scenario 中并尝试一下,这是一个提示 - 您可以快速进行实验,而无需像这样发出 HTTP 请求:

    * def response = { data: { name: 'Baelish of Harrenhal', user: 'griffin', id: '5c348c553a892c000bb1f2dd' } }
    * match response.data contains { id: '5c348c553a892c000bb1f2dd' }
    

    编辑:只是为了展示一些其他的断言方法:

    * match response.data.id == '5c348c553a892c000bb1f2dd'
    * match response..id contains '5c348c553a892c000bb1f2dd'
    
    * def id = { id: '5c348c553a892c000bb1f2dd' }
    * match response == { data: '#(^id)' }
    * match response contains { data: '#(^id)' }
    

    【讨论】:

    • 据我所知,我错过了 response.data?所以我需要告诉空手道这将是 JSON 有效负载而不是字符串?
    • @Glorifind 不,空手道默认理解 JSON。我只是用其他一些可能性编辑了答案。我建议阅读文档并很好地理解 JSON。然后是 Json-Path
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-09
    • 1970-01-01
    • 2021-08-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多