1

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

2
  • I tried to be short and concise which obviously did not work. I have added another, thank you Commented Jan 23, 2020 at 17:50
  • great, the whole point of a "simple example" is lost. anyway, see my answer Commented Jan 23, 2020 at 18:01

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.