空手道-匹配两个复杂的改组json

时间:2018-12-23 10:40:17

标签: karate

下面的问题与此非常相似:Karate - Validate json responses stored in different files我经历了建议的包含快捷方式,但找不到答案。

我需要比较两个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) -包含;但没有解决。我知道这一系列问题看起来很愚蠢,但我无法弄清楚行为!

1 个答案:

答案 0 :(得分:1)

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

'#(^^expectedList)' 

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