# Test fail due assertion problem

**URL:** <https://community.postman.com/t/test-fail-due-assertion-problem/29051>\
**Category:** Help Hub\
**Tags:** tests\
**Created:** [October 24, 2021, 6:53pm UTC](https://community.postman.com/t/test-fail-due-assertion-problem/29051 "2021-10-24T18:53:11Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![szczawik](https://avatars.discourse-cdn.com/v4/letter/s/6de8d8/32.png) [@szczawik](https://community.postman.com/u/szczawik)\
**Post date:** [October 24, 2021, 6:53pm UTC](https://community.postman.com/t/test-fail-due-assertion-problem/29051/1 "2021-10-24T18:53:11Z")

</div>

I have problem relating to assertion during practicing.

I have response body like:

[  
{  
“id”: 1,  
“manufacturer”: “Ford”,  
“model”: “Model T”,  
“build”: 1927  
},  
{  
“id”: 2,  
“manufacturer”: “Tesla”,  
“model”: “Model 3”,  
“build”: 2017  
},  
{  
“id”: 3,  
“manufacturer”: “Tesla”,  
“model”: “Cybertruck”,  
“build”: 2019  
}  
]

I wrote test:

const response = pm.response.json()

let model;

for (let filter of response) {  
if (filter.model === “Model 3”) {  
[//console.log](https://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

---

<div class="post-metadata">

**Author:** ![skandh-15](https://avatars.discourse-cdn.com/v4/letter/s/a3d4f5/32.png) [@skandh-15](https://community.postman.com/u/skandh-15)\
**Post date:** [October 25, 2021, 8:07am UTC](https://community.postman.com/t/test-fail-due-assertion-problem/29051/2 "2021-10-25T08:07:34Z")

</div>

Hi [szczawik](https://community.postman.com/u/szczawik),

Welcome to postman community 🙂

I tried the response you pasted seems like response is invalid, but what i can suggest you is since you response is array of objects.  
Try first

> console.log(response)  
> then try console.log(response[0][2].model)  
> if this gives you some model in console then try to loop it.

---

<div class="post-metadata">

**Author:** ![bpricilla](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/bpricilla/32/6511_2.png) [@bpricilla](https://community.postman.com/u/bpricilla)\
**Post date:** [October 25, 2021, 8:19am UTC](https://community.postman.com/t/test-fail-due-assertion-problem/29051/3 "2021-10-25T08:19:40Z")

</div>

@szczawik Welcome to the Community 🥳

When I started with Postman, parsing of the JSON response was so new to me. Later I found this [https://jsonpathfinder.com/](https://jsonpathfinder.com/). I wrote a blog about this [here](https://bpricilla.medium.com/how-to-easily-locate-your-json-object-in-your-api-response-647549ee8b2).

In your case, it should be

```auto
response[1].model

```

 ![image](https://us1.discourse-cdn.com/getpostman/original/2X/9/93733b1d82cb12a115a8482bae1bea01ebcf2cd8.png)

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:

```auto
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:

```auto
const response = pm.response.json()

pm.test("car model is Model 3", function () {
    pm.expect(response[1].model).to.eql("Model 3");

});

```

It depends on your requirement.

@skandh-15 Hello 👋

The JSON error in his sample response response is due to the improper quotes. You need to validate the JSON using any online tool.

@szczawik So before providing the JSON response/snippets please surround them using back ticks 🙌

I hope this helps 😊
