Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postman - How can I compare a message using vars

Tags:

postman

can anyone help me, I am almost new in Postman.

my issue is as follow:

  • I send a POST request and get a message asresponse:

    { "errorCode": 1000, "errorDescription": "Account is not verified by Admin!" }

  • this message is already saved in a var named "messageAccountIsnotVerified"

  • when I try to use comparison in postman tets script and compare the message with the expected string is working fine:
pm.test("test", function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData.errorCode).to.eql(1000);
    pm.expect(jsonData.errorDescription).to.eql("Account is not verified by Admin!");
});

But When I try to save the String text "Account is not verified by Admin!" in a variable named: messageAccountIsnotVerified and try to make the same comparison

pm.test("test", function () {
    var jsonData = pm.response.json();

    pm.expect(jsonData.errorCode).to.eql(1000);
    pm.expect(jsonData.errorDescription).to.eql("messageAccountIsnotVerified");
});

or

pm.test("test", function () {
    var jsonData = pm.response.json();
    var message = pm.environment.get("messageAccountIsnotVerified");
    pm.expect(jsonData.errorCode).to.eql(1000);
    pm.expect(jsonData.errorDescription).to.eql(message);
});

it failed with error:

test | AssertionError: expected 'Account is not verified by Admin!' to deeply equal 'messageAccountIsnotVerified'

Can someone explain to me 1. what does the "deeply equal" mean and 2. what do I wrong and 3. how can I use the assertion by using the variable

Thanks for any Hint

Just additional Info: I have the same issue when I compare email with @ sign in another message - so I assume may be something to do with special chars

like image 271
AKADO Avatar asked Oct 22 '25 04:10

AKADO


1 Answers

You need to get the variable in the following way:

pm.expect(jsonData.errorDescription).to.deep.equal(pm.environment.get("messageAccountIsnotVerified"))

.to.deep.equal can be used to reference something further down in a nested object.

Add .deep earlier in the chain to use deep equality instead. See the deep-eql project page for info on the deep equality algorithm: https://github.com/chaijs/deep-eql.

Looking at the response body you posted { "errorCode": 1000, "errorDescription": "Account is not verified by Admin!" }, I don't see a problem with the first test but for it to complain about deep equal then this response is probably no exactly as you posted.

Postman

You could reduce this all down again if you wanted too:

pm.test("test", () => {
    pm.expect(pm.response.json()).to.deep.equal({"errorCode": 1000, "errorDescription": "Account is not verified by Admin!"})
});

That test would do the same as above.

like image 148
Danny Dainton Avatar answered Oct 24 '25 20:10

Danny Dainton