Search code examples
conditional-statementskarateassertion

Karate assertion


Need to write two kind of assertions for the code below

  1. if “pay_eligible": false, then assert ineligibility is not null

“payment_data: { “pay_details": { “pay_eligible": false, "ineligibility: “balance” } }

  1. if pay eligible is true then ineligibility should not be populated

“payment_data: { “pay_details": { “pay_eligible": true, } }

* if ('pay_eligible' = false) karate.assert('ineligibility': '#notnull')

Solution

  • First, I strongly discourage "dynamic" tests like this, refer: https://stackoverflow.com/a/54126724/143475 - in other words, write each scenario so that you know exactly what the response should be and "hard code" it. Trust me on this, it will save you a lot of pain in the long run.

    That said, here is just one way to achieve what you want. More tips can be found here: https://stackoverflow.com/a/50350442/143475

    * def response = { pay_details: { pay_eligible: false, ineligibility: 'balance' } }
    * def expected = { pay_details: { pay_eligible: '#boolean' } }
    * expected.pay_details.ineligibility = response.pay_details.pay_eligible ? '#notpresent' : '#string'
    * match response == expected
    

    You might be able to reduce this further, but I leave it for you as an exercise: https://stackoverflow.com/a/70055035/143475