首页 > 解决方案 > 空手道 - 匹配两个复杂的洗牌 json

问题描述

下面的问题与此非常相似:空手道 - 验证存储在不同文件中的 json 响应我经历了建议的包含快捷方式,但无法找出答案。

我需要比较两个 json 文件,但使用 contains 关键字。为什么只包含?因为在某些情况下,我只需要匹配 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"
         },
         {
            "serviceDescription":"Checking SecondAddOn Service",
            "serviceCd":"PQRS",
            "randon":"FGDD"
         }
      ]
   }
}

我的代码:

* 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)'
* match Test1.webServiceSummary.includeServicesList == expected.webServiceSummary.includeServicesList

现在,上面的代码完美运行,我也得到了成功响应。但我担心的是我在contains any这里匹配。我应该用contains关键字验证。因为我需要确保所有参数expected.webServiceSummary.includeServicesList都存在于Test1.webServiceSummary.includeServicesList;不是其中的任何一个或一些。我尝试使用#(^expectedList)-- for contains; 但没有成功。我知道这一系列问题看起来很傻,但我无法弄清楚行为!

标签: karate

解决方案


这将始终检查 . 中contains only所有数组元素的值expectedList

'#(^^expectedList)' 

阅读文档:https ://github.com/intuit/karate#contains-short-cuts


推荐阅读