首页 > 解决方案 > 如何在许多对象中断言 2 个键

问题描述

回应 =

[
  {
    id: "1",
    modelId: "301",
    modelName: "x",
    trimId: "301",
    trimName: "up!",
    derivativeName: "xx",
    fuelType: "PETROL",
    tco: {
      price: {
        tco: null,
        retail: 12,
        offer: null,
        subsidy: null,
        residual: null
      },
      milesPerYear: 10000,
      mpg: 51.5,
      fuelCost: 1.1,
      milesPerKW: 0,
      monthsOfOwnership: 48,
      energyCost: 12,
      tax: 42,
      comparable: false
    }
  },
  {
    id: "1239",
    modelId: "301",
    modelName: "up!",
    trimId: "301",
    trimName: "x",
    derivativeName: "xx",
    fuelType: "PETROL",
    tco: {
      price: {
        tco: null,
        retail: 1,
        offer: null,
        subsidy: null,
        residual: null
      },
      milesPerYear: 10000,
      mpg: 53.2,
      fuelCost: 1.1,
      milesPerKW: 0,
      monthsOfOwnership: 48,
      energyCost: 12,
      tax: 4,
      comparable: false
    }
  }
]

一直在努力解决这个问题,但无法找出最好的方法。

我正在尝试检查何时 key fuelType= PETROL 然后 keyfuelCost必须等于 1.1

响应中有很多对象,我想在每个对象中声明 2 个键(fuelType 和 Fuelcost)。

我尝试了以下方法:

Given path 'url'
    When method GET
    Then match each response[*] contains {[*].fuelType:PETROL , [*].tco.fuelCost:1.1}

请帮忙谢谢

标签: karate

解决方案


试试这个,如果需要调整:

* def isValid = function(x){ return if x.fuelType == 'PETROL' && x.fuelCost == 1.1 }
* match each response == '#? isValid(_)'

推荐阅读