Postman is showing Assertion Error to check array property

230 views Asked by At

This is my JSON file:

{
"courses" : [
{
 "id": 1,
 "name": "John",
 "location": "India",
 "courses":["java", "selenium"] 
}
]
}

When I try to assert the array property, it shows Assertion Error. This is my Testing script:

var jsonData = pm.response.json();
pm.test("Test array properties", () => {
  pm.expect(jsonData.courses).to.include("java");
  pm.expect(jsonData.courses).to.have.members(["java", "selenium"]);
});

The error report is as follows:

Test array properties | AssertionError: the given combination of arguments (undefined and string) is invalid for this assertion. You can use an array, a map, an object, a set, a string, or a weakset instead of a string

But when I am adding the array directly in place of variable jsonData, this issue is not appearing. Here is the script:

var jsonData = pm.response.json();
pm.test("Test array properties", () => {
  pm.expect(["java", "selenium"]).to.include("java");
  pm.expect(["java", "selenium"]).to.have.members(["java", "selenium"]);
});

Please let me know how to fix this issue.

1

There are 1 answers

2
filoscoder On

Take a look at your JSON file carefully.

jsonData.courses do not have the value ["java", "selenium"]

jsonData.courses is an array of objects.

then jsonData.courses[0].courses is igual to ["java", "selenium"]