空手道-验证存储在不同文件中的json响应

时间:2018-12-22 16:53:50

标签: karate

我需要验证存储在两个不同文件中的2个json响应。我正在阅读并进行比较。但是我收到一个错误reason: all key-values did not match,并且几乎没有元素被匹配。仅当我将json存储在文件中并读入功能文件时,才会出现此问题。

Json文件1: Test.json

{
   "webServiceDetail":{
      "feature":{
         "featureCd":"ABCD",
         "imaginaryInd":"100.0",
         "extraInd1":"someRandomValue1"
      },
      "includefeatureList":[
         {
            "featureCd":"PQRS",
            "featureName":"Checking SecondAddOn Service",
            "extraInd1":"someRandomValue1",
            "extraInd2":"someRandomValue1"
         },
         {
            "featureCd":"XYZ",
            "featureName":"Checking AddOn Service",
            "imaginaryInd":"50.0"
         }
      ]
   }
}

Json文件2: Test1.json

{
   "webServiceSummary":{
      "service":{
         "serviceCd":"ABCD"
      },
      "includeServicesList":[
         {
            "serviceCd":"XYZ",
            "serviceDescription": "Checking AddOn Service"
         },
         {
            "serviceCd":"PQRS",
            "serviceDescription":"Checking SecondAddOn Service"
         }
      ]
   }
}

现在,我正在使用下面的代码来匹配两个json文件。

* def Test = read('classpath:PP1/data/Test.json')
* def Test1 = read('classpath:PP1/data/Test1.json')

* def feature = Test.webServiceDetail.feature
* set expected.webServiceSummary.service
| path               | value                |
| serviceCd          | feature.featureCd    |

* def mapper = function(x){ return { serviceCd: x.featureCd, serviceDescription: x.featureName} }
* def expectedList = karate.map(Test.webServiceDetail.includefeatureList, mapper)
* set expected.webServiceSummary.includeServicesList = expectedList
* print expected
* print Test1
* match Test1 == expected

下面是错误响应。

com.intuit.karate.exception.KarateException: Test.feature:35 - path: $, actual: {webServiceSummary={service={serviceCd=ABCD}, includeServicesList=[{"serviceCd":"XYZ","serviceDescription":"Checking AddOn Service"},{"serviceCd":"PQRS","serviceDescription":"Checking SecondAddOn Service"}]}}, expected: {webServiceSummary={service={serviceCd=ABCD}, includeServicesList=[[object Object], [object Object]]}}, reason: all key-values did not match

1 个答案:

答案 0 :(得分:1)

您接近,但includeServicesList的顺序不匹配。进行此更改:

* set expected.webServiceSummary.includeServicesList = '#(^^expectedList)'