JSON 使用 KARATE 进行响应验证

JSON Response validation with KARATE

我卡在验证以下响应中。

当我使用空手道发出获取请求时,我收到以下响应,我想验证它。

    My Response is: {
      "response": [
        {
          "tagName": "CaseTag",
          "value": "CaseTagMckAuto_TagValueOne",
          "entityType": "Case",
          "partitionId": 1,
          "appId": 1,
          "id": 46,
          "tagId": 1,
          "entityId": 1
        },
        {
          "tagName": "CaseTag",
          "value": "CaseTagMckAuto_TagValue",
          "entityType": "Case",
          "partitionId": 1,
          "appId": 1,
          "id": 45,
          "tagId": 1,
          "entityId": 1
        }
      ]
    }

我试过了:

And match response[0].tagName contains ['CaseTag']


Then match each res contains
    ...
           {
              {tagName: 'CaseTag', value: 'CaseTagMckAuto_TagValueOne', 
               entityType: 'Case', partitionId: 1, appId: 1,id: 46, 
               tagId: 1, entityId:1}
           }
    ...

And match response[0] == {tagName: 'CaseTag', value: 
      'CaseTagMckAuto_TagValueOne', entityType: 'Case', partitionId: 1, 
       appId: 1,id: 46, tagId: 1, entityId:1}

All three statements fails json it is not valid json array when i try giving

And match response == {tagName: 'CaseTag', value: 
      'CaseTagMckAuto_TagValueOne', entityType: 'Case', partitionId: 1, 
       appId: 1,id: 46, tagId: 1, entityId:1}        

它说它不是字符串。 你能帮我验证一下这个回复吗?

看来您对嵌套感到困惑。这是一个适合我的示例,只需将其粘贴到功能文件中,不需要 HTTP:

* def response = 
"""
[
   {
      "tagName":"CaseTag",
      "value":"CaseTagMckAuto_TagValueOne",
      "entityType":"Case",
      "partitionId":1,
      "appId":1,
      "id":46,
      "tagId":1,
      "entityId":1
   },
   {
      "tagName":"CaseTag",
      "value":"CaseTagMckAuto_TagValue",
      "entityType":"Case",
      "partitionId":1,
      "appId":1,
      "id":45,
      "tagId":1,
      "entityId":1
   }
]
"""
* match response == '#[2]'
* match response[0].tagName == 'CaseTag'
* match each response == { tagName: 'CaseTag', value: '#string', entityType: 'Case', partitionId: 1, appId: 1, id: '#number', tagId: 1, entityId: 1 }
* match each response contains { tagName: 'CaseTag', entityType: 'Case', partitionId: 1, appId: 1, tagId: 1, entityId: 1 }

看看你的 JSON 中有一个 response。如果真的是这样,就在使用match之前这样做,使它像我上面的那样:

* def response = response.response