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;
});
});