Paul D
Paul D

Reputation: 71

Cannot assert all elements in a single array with array that has no identifier name

response:

[
  {
    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
    }
  }
]

I'm trying to check when the key fuelType = PETROL then key fuel Cost must equal 1.1

I have tried the following

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

^^ But this just assert ONLY the top level element.

I have tried the following :

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

^^ But this throws a operand error.

I have about about 260 elements in a single array with no indentifeier name and I need to assert for ```fuelType == "PETROL" & fuelCost == 1.1 in all the 260 elements

Upvotes: 1

Views: 30

Answers (1)

Peter Thomas
Peter Thomas

Reputation: 58058

These 2 lines work perfectly:

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

You used a triple-equals which is wrong. Read the docs: https://github.com/intuit/karate#match-each

Upvotes: 1

Related Questions