Verify dynamic variable and body value in Test

Have variable name {{userName}} which i capture in pre-script as pm.environment.get("userName");

The printing works fine but Assertion error

pm.test("Check For legal name", function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData.Detail.legalName).to.eql(pm.environment.get("userName"));  
});

but gives Assertion error as
AssertionError: expected undefined to deeply equal ‘Test Firsname’

What is the syntax to make this test pass. @dannydainton

I love being tagged in things but there are more people who are able to help you on this Community forum. :smiley:

Without showing people what the response body is for that request - No one will know what jsonData.Detail.legalName is supposed to be referencing.

1 Like

Sorry Danny :slight_smile: i was just going to post it… i am printing wrong array here is the body.

"Status": true,
    "Message": "",
    "Detail": [
        {
            "legalName": "Test Firsname",
            "Surname": "Danny",
            "address": {
                "streetAddress": "Dr Helpful",
                "city": "Flower test",
                "state": "NY",
}
]

i think it should be

Detail[0].legalName

This works … Let me know its ok now? pm.expect(jsonData.Detail[0].legalName).to.eql(pm.environment.get(“userName”));

Only you would know if that’s OK now :joy:

If it solves the issues and your test is passing - I think you’re good. :trophy:

1 Like

Well Said :smiley: its ok …also verify with wrong test using surname… it fails so OK now and Sorry i tagged you… since you always answers first, reason for tagging you.

1 Like