robmcec
(Robert Minardi)
November 10, 2020, 10:34pm
1
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');
robmcec
(Robert Minardi)
November 10, 2020, 11:24pm
3
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
:
robmcec
(Robert Minardi)
November 12, 2020, 5:11pm
5
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'
arlem
(Arlémi Turpault)
November 12, 2020, 5:28pm
6
@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
.
robmcec
(Robert Minardi)
November 12, 2020, 5:53pm
8
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!
arlem
(Arlémi Turpault)
November 13, 2020, 2:57pm
9
I haven’t tried but looking at the documentation a combination of include
and keys
should do the trick!
I’d also recommend that cheat-sheet, it came in very handy a few times for me:
ultimate-ut-cheat-sheet.md
# The Ultimate Unit Testing Cheat-sheet
<p align="center">
<img src="https://res.cloudinary.com/yoavniran/image/upload/q_auto,w_400,f_auto/v1603209048/uutcs-logo_zunwh4" alt="uutcs-logo"/>
</p>
___
* [Sinon Chai](#sinon-chai)
* [Chai](#chai)
* [Sinon](#sinon)
This file has been truncated. show original