I’m looking for a chained postman chai bdd assertion where test should PASS if the response body contains specific key value pairs which happens to be within a nested JSON Array. If not possible to have single chained assertion, happy to split it into multiple assertions for each reason code as per their existence requirement.
For Example: Say below is the JSON response body
{
“item”: {
“code”: “A1234Z”,
“name”: “Sample Item Name”,
“status”: “REJECTED”
},
“price”: {
“baseAmount”: “10.0”,
“pricingType”: “RRP”
},
“reasonCodes”: [
{
“code”: “123”,
“text”: “This is a REJECT reason code 123.”,
“type”: “R”
},
{
“code”: “456”,
“text”: “This is a WARN reason code 456”,
“type”: “W”
},
{
“code”: “789”,
“text”: “This is a INFO reason code 789”,
“type”: “I”
}
]
}
Criteria: Test should pass if the reasonCodes Array contains codes 456 & 789, but not 123.
Any help is much appreciated. I’ve attached a sample trial I’ve done using include.deep.members, where I’m able to get what I wanted if the array contains only code elements in it without any other elements. See the screenshot attached. image|690x399
Do you have a set of reason codes you specifically do not want to allow? That would probably be the easiest thing to do. I might structure it like this:
const invalidReasonCodes = ['123'];
pm.test("Does not have any invalid codes", function () {
const jsonData = pm.response.json();
const invalidCodes = jsonData.data.reasonCodes.filter(rc => invalidReasonCodes.indexOf(rc.code) > -1);
pm.expect(invalidCodes.length).to.eql(0);
});
Thanks for the reply, but my test pass criteria is that the reasonCodes Array must contain codes 456 & 789, but not code123. Also the invalid codes can be single or multiple at times.
To simplify my query, if response json is an array as below then test must PASS if codes 1 & 2 are found and 4 & 5 are not found in it.