Search code examples
karateassertion

In Karate, how to write an assertion that when i loop thru belowarray, all the IDs should have name associated to them except for the id "nonexisting"


“History: { “Count”: 6, "summary": [ { "id": "5010425959", "name": "Clothes", "total_amount": "-105.23" }, { "id": “NONEXISTING”, "total_amount": "-3307.03" }, { "id": "5010425965", "name": "Cell Phone", "total_amount": "196.22" }, { "id": "5010425964", "name": "Dogs", "total_amount": "454.40" }, { "id": "5010425962", "name": "Coffee", "total_amount": "25.00" }, { "id": "5010425958", "name": "Entertainment", "total_amount": "33.48" }, { "id": "502304430", "name": "Groceries", "total_amount": "109.24" } ] } }

I need to write a condition that if name exists for the id "NONEXISTING" then the TC should fail


Solution

  • There are many way to do this, just giving one option below:

    * def existing = response.filter(it => it.name)
    * match each existing contains { name: '#string' }
    * def nonexisting = response.filter(it => it.id == 'NONEXISTING')
    

    Refer here if you want more explanation: https://stackoverflow.com/a/62567262/143475