karateweb-api-testing

How to match that the API response has one of the possible value using Karate?


I have a response which I would like to validate in such a way that a particular attribute in this case capital may contain any of the following value {"BOM", "DEL", "BLR"}

Here is my example -

* def cityDetails =
"""
[
 {
  "state":"KA",
  "capital":"BLR",
  "population":"1M"
 },
 {
  "state":"MH",
  "capital":"BOM",
  "population":"1.5M"
 }
]
"""

When I used the following match statement -

* match each cityDetails[*].capital contains any ['BOM', 'DEL', 'BLR']

It does not give me the desired assertion. What possible mistake I may be making here? Please guide.


Solution

  • Personally, I discorage such "clever" tests, explained here: https://stackoverflow.com/a/78131074/143475

    But if you insist, refer https://stackoverflow.com/a/77851131/143475

    Here's one way to do it - it may not be the best way but will just give you ideas:

    * def data = ['BLR', 'BOM', 'DEL']
    * def capitals = cityDetails.map(x => x.capital)
    * def valid = capitals.filter(x => data.includes(x))
    * match capitals == valid