【问题标题】:is it possible to extract the value of one of the key value pairs in request to be asserted是否可以在要断言的请求中提取其中一个键值对的值
【发布时间】:2020-01-16 16:22:08
【问题描述】:

peter 为我提供了使用 karate 0.9.3 应用示例断言的解决方案

Trying to do some assertion from request, which will be present in response

我想知道是否可以从请求而不是完整请求中断言一个值。

**Background:**
*  configure headers = read('classpath:merchantServiceheaders.js')

   Given url MservUrl 
   And path 'XXXXXX'

   And request Data
   When method POST 
   Then status 200 
   And match response != null 
   And match $ contains {serviceName: 'XXXXX'Service', responseMessage: 
   'Success' }
   And match each $.clauses..responseMessage == 'Success'
   And match each $..predicate != null
   And match each $..predicate == '#present'
   And match each $..predicate == '#regex [^0-9]+[0-9]*$'
   And match $..predicate contains Data.clauses.subject

   Examples: 
   |Data!                                               |
   |'{"clauses":[{"subject":"XXXX","predicate":"999999"}, 
   {"subject":"XXXXX","predicate":"99999"}]}'|

我要做的是在 并且匹配 $​​..predicate 包含 Data.clauses.subject

这可能吗?

【问题讨论】:

    标签: karate


    【解决方案1】:

    是的,如果将Data 定义为变量,则很有可能。但请注意,$..predicate 将始终是 JSON 数组:https://github.com/intuit/karate#get-plus-index

    如果您需要帮助,请创建一个合适的简单工作示例。

    【讨论】:

      【解决方案2】:

      示例代码:

      Feature: Validation
      
          Scenario:
      
              * def resp = {predicate : "4325325456545646"}
              * def data =
                  """
                  {
                      "clauses": [
                          {
                              "subject": "5432154321543210",
                              "predicate": "4432154321543210"
                          },
                          {
                              "subject": "4325325456545646",
                              "predicate": "4325325456545646"
                          }
                      ]
                  }
                  """
              * def sublist = get data.clauses[*].subject
              * print sublist
              * match sublist contains resp.predicate
      

      【讨论】:

        猜你喜欢
        • 2014-02-05
        • 2021-03-24
        • 1970-01-01
        • 1970-01-01
        • 2023-01-24
        • 2019-02-26
        • 2018-10-24
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多