Check field in response

Your question may already have an answer on the community forum. Please search for related topics, and then read through the guidelines before creating a new topic.

Hereā€™s an outline with best practices for making your inquiry.

My question: I have a problem with response in postman in grpc. I would like to check if the field is returned in the response for all elements.

Details (like screenshots):This is my code:

pm.test(ā€˜instrumentNames is arrayā€™,()=> {

for (var i = 0; i < investors.length; i++) {
let investor = investors[i];
var instrumentNames=pm.response.messages.all()[0].data.investors[i].instrumentNames;

pm.response.messages.to.have.property(ā€˜investors[i].instrumentNamesā€™)

pm.expect(instrumentNames).to.be.an("array")

}

});

And this is error: instrumentNames is array | AssertionError: expected { investors: [ { ā€¦(7) } ], ā€¦(1) } to have deep nested property ā€˜investors[i].instrumentNamesā€™

This is response:
{
ā€œinvestorsā€: [
{
ā€œinstrumentNamesā€: [
ā€œEEEEā€,
ā€œAASā€

        ],
        "cdbss": "2222",
        "Name": "AAAAA",
        "avatarUrl": "ssss"
        "isMyProfile": false,
        "rScore": 10,
        "return": "11"
    }
],
"result": "RESULT_OK"

}

Hi @piotrurbanek94 !

Can you put your code into code blocks for me? It looks like thereā€™s some syntax errors with the JSON you provided

{
    "investors": [
        {
            "instrumentNames": [
                "EEEE",
                "AAS"
                
            ],
            "cdbss": "2222",
            "Name": "AAAAA",
            "avatarUrl": "ssss" // ** No comma **
            "isMyProfile": false,
            "rScore": 10,
            "return": "11"
        }
    ],
    "result": "RESULT_OK"
}

Based on your code snippets, it looks like the error youā€™re experiencing is due to the way youā€™re trying to check the property ā€œinstrumentNamesā€. Youā€™re using a string investors[i].instrumentNames which is not being interpreted as you expect. The string is not replaced with the actual value of i from the loop, hence it is not a valid property path.

In order to correctly loop through each investor and check for the presence of the ā€˜instrumentNamesā€™ property, you should dynamically build the property path string by concatenating the string ā€˜investors.ā€™ with the value of i and ā€˜.instrumentNamesā€™.

pm.test('instrumentNames is array',()=> {
  let investors = pm.response.json().investors;

  for (var i = 0; i < investors.length; i++) {
    let investor = investors[i];
    let instrumentNames = investor.instrumentNames;
    
    // Build the property path string dynamically
    let propertyPath = 'investors.' + i + '.instrumentNames';

    pm.expect(pm.response.json()).to.have.nested.property(propertyPath);
    pm.expect(instrumentNames).to.be.an("array");
  }
});

Hello Kevin,

Thank you for your answer. I checked my response and itā€™s ok.

You have a right. This code working:

pm.test(ā€˜response body has fields instrumentNames and cdbiD returnedā€™, () {
pm.response.messages.to.have.property(ā€˜investors[0].instrumentNamesā€™);
pm.response.messages.to.have.property(ā€˜investors[0].cbbidā€™);
});

When I use loop itā€™s a problem. I try also used this code :

pm.test(ā€˜instrumentNames is arrayā€™,()=> {
let investors = pm.response.messages.all()[0].data.investors;

for (var i = 0; i <= investors.length; i++) {
let investor = investors[i];
let instrumentNames =pm.response.messages.all()[0].data.investors.instrumentNames;

// Build the property path string dynamically
let propertyPath = 'investors.' + i + '.instrumentNames';

pm.expect(pm.response.messages.to.have.nested.property(propertyPath));
pm.expect(instrumentNames).to.be.an("array");

}
});

But i have error AssertionError: expected undefined to be an array.

let instrumentNames = pm.response.messages.all()[0].data.investors.instrumentNames;

This wouldnā€™t work because investors is an array which does not have a property, instrumentNames. Thatā€™s why itā€™s returning undefined in your assertion. Youā€™d have to access an element, i, to access the instrumentNames property of the contained object.

For example, this should work:

let instrumentNames = pm.response.messages.all()[0].data.investors[i].instrumentNames;

but since you already defined investors and investor, you can use let instrumentNames = investor.instrumentNames;.

@piotrurbanek94

Here is another solution which I think covers your tests.

const response = pm.response.json().data;

pm.test("Expect Investors array to exist", () => {
    pm.expect(response.investors).to.be.an("array").that.is.not.empty;
})

// We are working with arrays, so I find forEach loops easier to define and work with
response.investors.forEach(investor => {

    // I've separated the tests, and also included string literals to customise the test case
    // in the loop, otherwise if you have a lot of records it can be difficult to work out
    // which one has failed
    pm.test(`expect ${investor.Name} to include cdbss property`, () => {
        pm.expect(investor).to.have.property('cdbss').and.to.not.equal(null).and.not.be.undefined;;
    });

    // Check that the instrumentNames property exists first, and then check its an array
    pm.test(`expect ${investor.Name} to include instrumentNames array`, () => {
        pm.expect(investor).to.have.property('instrumentNames').and.to.not.equal(null).and.not.be.undefined;;
        pm.expect(investor.instrumentNames).to.be.an("array").that.is.not.empty;
    });

});