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