What is wrong with pm.expect

let jsonData = pm.response.json()
for (var i=0; i<count; i++){
pm.test(“To check if all keys are present and value is NOT empty for eating”, function () {
console.log(“start_date”, jsonData.data[i].start_date)
console.log(“score”,jsonData.data[i].score)
console.log(“unit”,jsonData.data[i].unit)
console.log(“status”,jsonData.data[i].status)
pm.expect(jsonData.data[i]).to.have.property(start_date,score,unit,status);
});
}

Console is printing data but when I am saying pm.expect giving error saying
To check if all keys are present and value is NOT empty for eating | ReferenceError: start_date is not defined

Here is the response
{
“pet_id”: 119517,
“start_date”: “2021-10-27”,
“end_date”: “2021-11-02”,
“num_of_days”: 3,
“score”: 0,
“unit”: “second”,
“status”: “not_enough_data”,
“data”: [
{
“start_date”: “2021-10-27”,
“score”: 0,
“unit”: “second”,
“status”: “below_average”
},
{
“start_date”: “2021-10-28”,
“score”: 0,
“unit”: “second”,
“status”: “below_average”
},
{
“start_date”: “2021-10-29”,
“score”: 0,
“unit”: “second”,
“status”: “below_average”
}
]
}

  1. Check if start_time property is there or not?
  2. If yes, then probably you need to open console and see the response that you get and check spelling.

@skandh-15 my mistake I posted incorrect script, but yes while printing it’s printing data but error only in pm.expect.
Still seeing same error
To check if all keys are present and value is NOT empty for eating | ReferenceError: start_date is not defined

Hey @hasmukhpatel!

Seems like a problem with fetching start_date value from your json response, if you could also share the response, it will help the community better understand where the problem exists in your code.

Good luck!

@bbahadur I added response to the original post. Thanks for looking into it.

Hey @hasmukhpatel !

If you want to check if ‘data’ has all properties, you can iterate through each and perform assertion. Here is one way to achieve it:

let jsonData = pm.response.json();

console.log(jsonData.data);

pm.test("All keys present",()=>{
_.each(jsonData.data, (item)=>{
    
    pm.expect(item.hasOwnProperty('start_date')).to.be.true;
    pm.expect(item.hasOwnProperty('score')).to.be.true;
    pm.expect(item.hasOwnProperty('unit')).to.be.true;
    pm.expect(item.hasOwnProperty('status')).to.be.true;
})

})

Similarly you can even add assertions to check for presence of value.

Hope it helps,
Good luck!

1 Like

@bbahadur yaaaaa, it worked. Thanks Bharat for your time and guidance. I can say I can go one step ahead in postman.

1 Like