首页 文章

空手道中的JSON ResponseSchemaValidation

提问于
浏览
1
"totalRows": 1,
  "colDefs": [
    {
  "entityAttributeId": "acctNm",
  "headerName": "Account Name",
  "field": "2",
  "entityPath": "",
  "entityId": "account"
},
{
  "entityAttributeId": "acctId",
  "headerName": "Account ID",
  "field": "1",
  "entityPath": "",
  "entityId": "account"
}
],
"rowData": [
{
  "1": "1939",
  "2": "Bay Pond Partners",
  "rowMeta": {
    "account": {
      "acctInstrumentId": "0025-1939",
      "acctId": "1939"
    }
  }
  }
]
}

我有一个过滤查询的响应 . 由于过滤器应该只返回一个值,我正在验证:并且匹配GetDataSet_Response包含{“totalRows”:1}

过滤器基于Acctid . 现在,我需要验证acctID值和整个json模式 . 我怎么能在KARATE做到这一点?

1 回答

  • 2

    在Schema中,您可以定义一个

    • 您期望但不知道确切值的值的数据类型 .

    • 为数据写入预期条件 .

    • 定义您已经知道的数据或硬编码数据的变量 .

    • 忽略一些不打扰的数据 .

    • 分解您的JSON并创建多个逻辑模式(从您的JSON中,您可以为"colDefs"和"rowData"设置单独的模式)

    以及更多复杂验证的选项 .

    我不确定您的确切要求,我尝试创建可能符合您要求的架构 .

    {
      "colDefs": [
        {
          "entityAttributeId": "acctNm",
          "entityId": "account",
          "entityPath": "",
          "field": "2",
          "headerName": "Account Name"
        },
        {
          "entityAttributeId": "acctId",
          "entityId": "account",
          "entityPath": "",
          "field": "1",
          "headerName": "Account ID"
        }
      ],
      "rowData": [
        {
          "1": "#string",
          "2": "#string",
          "rowMeta": {
            "account": {
              "acctId": "#(acctID)",
              "acctInstrumentId": "#string"
            }
          }
        }
      ],
      "totalRows": 1
    }
    

    您可以将模式存储在JSON文件中/直接在脚本中定义它,并将其与您的响应相匹配

    假设我们将此架构存储在一个文件中 filterResponseSchema.json

    YourFilter.feature

    * def acctID = "1939"
    # call your filter request
    * def myFilterSchema = read('filterResponseSchema.json')
    * match response == myFilterSchema
    

    Note:

    i)在调用此模式之前确保存在变量名称“acctID”,以便空手道将该值嵌入到JSON模式中 .

    ii)我对你的“colDefs”值的假设总是一样的,所以我硬编码了 .

    Karate documentation涵盖 Schema validation 的大量示例

    我建议你阅读本文以获得更多见解 .

相关问题