使用空手道进行 JSON 响应验证

Say*_*yed 0 json 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
        }
      ]
    }
Run Code Online (Sandbox Code Playgroud)

我试过:

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}        
Run Code Online (Sandbox Code Playgroud)

它说它不是字符串。你能帮我验证这个响应吗?

Pet*_*mas 5

看起来你对嵌套感到困惑。这是一个适用于我的示例,只需将其粘贴到功能文件中即可,无需 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 }
Run Code Online (Sandbox Code Playgroud)

查看您的 JSON 中如何包含 a response。如果确实如此,请在使用match,之前执行此操作,使其像我上面所说的那样:

* def response = response.response
Run Code Online (Sandbox Code Playgroud)