空手道-验证存储在不同文件中的json响应 [英] Karate - Validate json responses stored in different files

查看:44
本文介绍了空手道-验证存储在不同文件中的json响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to validate 2 json responses which are stored in two different files. I am reading them and comparing. But i get an error reason: all key-values did not match and there're few elements not being matched. This problem occurs only when i store the json in a file and read in my feature file.

Json文件1: Test.json

Json File 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

Json File 2: Test1.json

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

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

Now, I am using below code to match two json files.

* 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

推荐答案

您很接近,但是includeServicesList的顺序不匹配.进行以下更改:

You are close, but the order of includeServicesList is not matching. Make this one change:

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

这篇关于空手道-验证存储在不同文件中的json响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆