Expect array property test failing

Here’s my repsonse:

{
    "data": {
        "statusItem": [
            {
                "ID": 53,
                "cost": 0,
                "discountType": 0,
                "discountValue": 0,
                "flags": 96,
                "flags3": 256,
                "flags4": 0,
                "flags5": 0,
                "futureBreakID": 0,
                "group": 4,
                "highlightColor": 0,
                "inActive": 0,
                "miscItemID": 0,
                "name": "Acct Correction",
                "notificationID": 0,
                "overrideRejectMins": false,
                "POReturnReasonID": 0,
                "priority": 0,
                "rejectStatusMins": 0,
                "seqNum1": 0,
                "seqNum2": 8192,
                "userOrGroupID": 0
            }

I’m trying to expect that just the property ID is in the response.

When I try this:

pm.expect(jsonData.data.statusItem[0]).to.include("ID");

I get the following error:
Test array properties | AssertionError: object tested must be an array, a map, an object, a set, a string, or a weakset, but object given

This does work

pm.expect(jsonData.data.statusItem[0]).to.include({"ID":53});

I understand that the array contains objects, but I don’t know how to isolate the property in the object.

Is there a reason that you have gone for include over something else?

Maybe keys or property might work better:

.to.have.property('ID');

Yeah, I found that like 5 minutes ago…

Is there a way to chain those together so it’s kind of like a poor man’s schema validation?

pm.expect(jsonData.data.statusItem[0]).to.have.property("ID");
pm.expect(jsonData.data.statusItem[0]).to.have.property("cost");

Is there a way to make this less verbose?

I’ve tried a few different ways with and and it fails.

Thanks!

Have you tried any other method from the chaiJS link, like maybe keys:

Yes I have.
This test

pm.expect(jsonData.data.statusItem[0]).to.have.keys('ID'); 

Gives this error

Test array properties | AssertionError: expected { Object (ID, cost, ...) } to have key 'ID'

@robmcec This should work better:
pm.expect(jsonData.data.statusItem[0]).to.have.any.keys('ID');

If you want to check all properties, you can use all instead of any.

Is there a way to check only some keys?

Say I want to check for only ID and Cost I do this:

pm.expect(jsonData.data.statusItem[0]).to.have.property("ID");
pm.expect(jsonData.data.statusItem[0]).to.have.property("cost");

If I do that with this:

pm.expect(jsonData.data.statusItem[0]).to.have.any.keys("ID","cost");

It works, but so does this:

pm.expect(jsonData.data.statusItem[0]).to.have.any.keys("ID","adsfasdfasd");

Because ID and that’s considered “any”.

Is there a way to say

pm.expect(jsonData.data.statusItem[0]).to.have."some".keys("ID","cost");

I know that syntax is wrong, but “any” keys still only helps me if only one is correct. I need a way to list the keys that I want to check on (not all of them, just some) and return a fail if that key\property doesn’t exist.

Thanks!

I haven’t tried but looking at the documentation a combination of include and keys should do the trick! :slightly_smiling_face:

I’d also recommend that cheat-sheet, it came in very handy a few times for me: