AssertionError: expected ' ' to deeply equal

I’m validating one variable value in the response of Postman.
Here is my code:
var jsonArrayData = pm.response.json()

console.log(jsonArrayData[0].fields.externalid);

pm.test("Response body contains "+ jsonArrayData[0].fields.externalid, function(){

pm.expect(jsonArrayData[0].fields.externalid).to.eql(pm.collectionVariables.get("Customer_Number_Netsuite"));

});

and here is the response:

Response body contains 283670438065 | AssertionError: expected ‘283670438065’ to deeply equal 283670438065

Also, find the screenshot for reference:

Hi @jagrutiwani0, this error is because you are comparing two different data types. Even the value is same here the data type is different.

So while storing the collection variable you can store using JSON.stringify() method,

else you can convert it to integer like this post.

pm.expect(jsonArrayData[0].fields.externalid).to.eql(parseInt(pm.collectionVariables.get("Customer_Number_Netsuite")));

Please try and let me know if this works :blush:

6 Likes

Hey, it’s working now. I converted both the variables to Integer. Thanks :slight_smile:

1 Like

Hi @bpricilla ,

What will I use for the comparison of string values?
Now I’m getting Response body contains Company Name asTestAPI49514 | AssertionError: expected ‘TestAPI49514’ to deeply equal undefined error.

@jagrutiwani0 You need to use JSON.stringify() method.

But in the above error it states that the variable is undefined.

1 Like

Hi @bpricilla ,

Thanks for noting it! The issue was with the variables. The same were stored in the environment as well as collection. So removed the one from the environment and keeping those variables in Collection. The assertion is working even without using JSON.stringify()

1 Like

Okay good to know that. All the best :bouquet: Let us know if you have any other issues.

1 Like
I am trying to write some tests for Day :08 Run a collection

This is my response body:
{
    "results": [
        {
            "gender": "female",
            "name": {
                "title": "Miss",
                "first": "Louna",
                "last": "Fournier"
           },
            "location": {
                "street": {
                    "number": 7581,
                    "name": "Rue Principale"
                },
                "city": "Villeurbanne",
                "state": "Cher",
                "country": "France",
                "postcode": 30638,
                "coordinates": {
                    "latitude": "-24.9843",
                    "longitude": "43.4648"
                },
                "timezone": {
                    "offset": "-2:00",
                    "description": "Mid-Atlantic"
                }
            },
            "email": "louna.fournier@example.com",
            "login": {
                "uuid": "526cff23-7dbb-484e-a2f5-755666e569e3",
                "username": "yellowfrog507",
                "password": "bugs",
                "salt": "CYRTGp3l",
                "md5": "e1db3a070c437203833aeb489ee03743",
                "sha1": "c06d4a9517092b57bbe1ea090914b77f11d7ab77",
                "sha256": "6d4db16a4a030d9fa56e0695d62178130dc69b2c1124796e04bf6cce347447cb"
            },
            "dob": {
                "date": "1962-10-08T00:07:20.111Z",
                "age": 59
            },
            "registered": {
                "date": "2008-08-08T05:24:26.268Z",
                "age": 13
            },
            "phone": "02-39-34-68-25",
            "cell": "06-12-23-24-24",
            "id": {
                "name": "INSEE",
                "value": "2NNaN79081850 66"
            },
            "picture": {
                "large": "https://randomuser.me/api/portraits/women/36.jpg",
                "medium": "https://randomuser.me/api/portraits/med/women/36.jpg",
                "thumbnail": "https://randomuser.me/api/portraits/thumb/women/36.jpg"
            },
            "nat": "FR"
        }
    ],
    "info": {
        "seed": "ba66a06ae496d715",
        "results": 1,
        "page": 1,
        "version": "1.3"
    }
}

and my test is

pm.test("Person is Female", () => {
 const responseJson = pm.response.json();
  pm.expect(responseJson.nat).to.eql("FR");
});

Error: Person is Female | AssertionError: expected undefined to deeply equal ‘FR’

Hello @hameethapostman,

pm.test("Person is Female", () => {
 const responseJson = pm.response.json();
  pm.expect(responseJson.results[0].nat).to.eql("FR");
});

Please try this snippet. undefined means that the element is not reachable, so the path might be wrong.

Just a tip here, you can always print your response in console and see the array structure, else for newbies this site will be really helpful.

You just need to paste your JSON here and you will get the path as result, like below. I have tried with your JSON response.

I hope this will help you better!! All the best :hugs:

4 Likes

Just to add to @bpricilla’s excellent answer: here’s one thing that won’t cause the test to fail, but (in the real world) might cause you a headache later -

Your test is called Person is Female but your assertion is checking the nationality. It’s a good idea to make sure your test names are reflective of what is actually being tested (e.g. Person is French), so that if the test fails, you can more easily debug the problem :slight_smile:

2 Likes

The solution worked Thanks , I see a different error now.

Hello @hameethapostman ,

I can provide a hint here, please go the Tests tab of the submit request.

You can locate your failed Test case like above and check into the details if you have done the actions marked inside here. And based on you error message it is pretty straight forward that some query params are missing in above any of the three requests. Please try to go through again the documentation details. You can fix it :blush:

Still unable to find the error, I have set the necessary parameters yet see this error.

@hameethapostman Please check the URL in the second screen shot, it should only be ‘fr’ and not ‘FR’ :slightly_smiling_face:

@bpricilla I did have only FR in the url first ,then I thought may be it is looking for both upper and lowercase and changed it, but still no luck. Don’t know where I am missing it.

These are the scripts I have written for all the three tests



Requests added correctly | AssertionError: check gender param key: expected ‘nat’ to equal ‘gender’

@hameethapostman ,

A general suggestion here, whenever you are stuck-up first thing would be to start over the documentation and read word by word in a fresh mindset :blush: Trust me this worked for me many times.
Because sometimes a small typo will be killiing our time :sweat_smile:

So in your case, please go and read your documentation again!

image

Your response should have the nat as ‘FR’ and the URL should have in lower case like in the documentation.

I hope this helps :blush:

1 Like

Thanks @bpricilla rereading documentation several times worked for me. The actual problem was the order of parameters in the third request was wrong

Actual : https://randomuser.me/api/?nat=fr&gender=female

Expected was to assert that the user was a female first and then nationality was French.
https://randomuser.me/api/?gender=female&nat=fr

@hameethapostman , the order of the params shouldn’t matter here. Both of them should be working and return the same results.

2 Likes

Hey, I am getting the similar kind of error for below code

pm.globals.set('respb', pm.response.json());

if (!Object.is(pm.globals.get('respb'), pm.globals.get('respa'))) {

    const result = diff(pm.globals.get('respb'), pm.globals.get('respa'));

    console.log(result);

    pm.test('Difference' + JSON.stringify(result), () => {

    pm.expect(0).to.equal(pm.globals.get(1));

    //pm.expect(0).to.equal(parseInt(pm.globals.get(1)));

});

}

Can someone help me on this?
Thanks in advance.

Hi @Raja2242

You have to assert using “eql” as below,

pm.expect(0).to.eql(pm.globals.get(1));