1

I want to write a test to validate the number of objects nested in "children" is equal to what I am expecting. Let's say in this case it is 2.

This is the response body I am testing against:

{
    "result": {
        "line_item": {
            "name": null,
            "description": "abc",
            "quantity": 1.0,
            "children": [
                {
                    "sku": "xxx",
                    "quantity": 1.0,
                    "description": "abc"
                },
                {
                    "sku": "yyy",
                    "quantity": 3.0,
                    "description": "def"
                }
        ]
    }       
  }
} 

I have tried a few things, one of them being this:

let josnData = pm.response.json();

var children = jsonData.result.line_item.chilren;

var length_children = children.length;

pm.test("Only 2 objects in Children", function (){
    pm.expect(length_children).to.eql(2);
});

This just throws an error. Would appreciate any ideas how to tackle this. Very new to testing in postman and eager to learn. Thanks!

2 Answers2

0

I figured it out. But definitely interested in other ways. Here is what I did.

var body = JSON.parse(responseBody);
var list = body.result.line_item.children.length;

pm.test("Only 2 objects in children", function(){
    pm.expect(list).to.eql(2);
    });
0

This probably would have done the same thing as your solution:

let children = pm.response.json().result.line_item.children;

pm.test("Only 2 objects in Children", function () {
    pm.expect(children.length).to.eql(2);
});
Danny Dainton
  • 23,069
  • 6
  • 67
  • 80