Validate actual with expected array values

Hi,

Sorry but I searched the community but could not find a fix hence troubling the experts here.
One of my API has a json response body as below:
{
“suggestions”: [
{
“value”: “jacket”
},
{
“value”: “jacquard-knit socks”
},
{
“value”: “jacquard-knit jumper”
},
{
“value”: “jacquard-weave cotton rug”
},
{
“value”: “jacquard-weave skirt”
}
]
}

Now I need to validate each value of key “value:” with expected data set. should I create data set variables in Predefined script ? Not sure how this can be done, please suggect/help.

I would do something like this:

const response = {
	"suggestions": [{
			"value": "jacket"
		},
		{
			"value": "jacquard - knit socks"
		}
	]
}

pm.test("has suggestions", () => {
    const suggestions = response.suggestions.map((item) => item.value);
    pm.expect(suggestions).to.have.members(["jacquard - knit socks", "jacket"]);
});
1 Like

Thanks for the reply,

i used pm.test(“Body matches string”, function () {
pm.expect(pm.response.text()).to.include(“jacket”,“jacket super skinny fit”,“jacket skinny fit”,“jacket slim fit”,“jacket with faux fur lining” );

To extend my test, how do I compare my response array with already defined data of array.
For eg, send in jeans and get above. Would like to test with more data, how do I achieve that. please help.

You need to consider that your test is weaker as it searching the entire response, with no regard to the underlying data structure.

Dont really understand how my test is weak considering that I input a search and result is an array of values. I need to validate those values depending upon some predefined expected data.

Is there a way to create a mapping? or you suggest how to make my test realistic.