for (let filter of response) {
if (filter.model === “Model 3”) { //console.log(filter)
model = filter;
}
}
console.log(model)
pm.test(“car model is Model 3”, function () {
pm.expect(response.model).to.eql(“Model 3”);
});
Unfortunately, my test fail AssertionError: expected undefined to deeply equal ‘Model 3’ I was trying to figure out but dunno why this is not passing because my navigation to that model is correct. Can you guide me what is wrong with my code please
When I started with Postman, parsing of the JSON response was so new to me. Later I found this https://jsonpathfinder.com/. I wrote a blog about this here.
Try exploring this tool. Also in your snippet you have used filter method. So are you trying to compare the response directly against the value or you need to filter the model 3 and trying to validate that against your expected value (Model 3). In that case I did minor tweaks to your snippet as below:
const response = pm.response.json()
let model;
for (let filter of response) {
if (filter.model === "Model 3") {
//console.log(filter)
model = filter.model;
}
}
//console.log(model);
pm.globals.set("modelName", model);
pm.test("car model is Model 3", function () {
pm.expect(pm.globals.get("modelName")).to.eql("Model 3");
});
Else you can directly validate the response against the expected value:
const response = pm.response.json()
pm.test("car model is Model 3", function () {
pm.expect(response[1].model).to.eql("Model 3");
});