How can Postman compare the value of a written variable with the value from json?

The “id_user” variable has the value “3”. the “id” from json is equal to “3”.

pm.expect(json Data.id).to.eql("{{id_user}}");

Using this command, I try to compare values. Returns an error:

AssertionError: expected 3 to deeply equal ‘{{id_user}}’

If the code above is replaced with:

pm. expert(json Data.id).to. eql(3);

Everything works.

Your question may already have an answer on the community forum. Please search for related topics, and then read through the guidelines before creating a new topic.

1 Like

Hey @alexei.taushkanov

The {{...}} syntax isn’t available in the sandbox to use directly in the expect statement like that.

You would need to use it like this:

pm.expect(jsonData.id).to.eql(pm.environment.get("id_user"));

Alternatively, that double curly brace syntax could be used like this:

pm.expect(jsonData.id).to.eql(pm.environment.replaceIn("{{id_user}}"));

5 Likes

@danny-dainton
Returns an error: Your test name | AssertionError: expected 3 to deeply equal ‘3’. =(
Is it possible that the value “id_user” was written in string format?

That can be handled like this, it will turn the variable saved as a string into an integer.

pm.expect(jsonData.id).to.eql(parseInt(pm.environment.get("id_user")));

4 Likes

this doesn’t seem to work for me.
For a simple comparison of 2 integer variables, what works?
var numEventsExpected = 2;
var numEventsReceived = 1;

console.log(" expected " + parseInt(numEventsExpected)) ;
console.log(" received " + parseInt(numEventsReceived)) ;
??? pm.expect(parseInt(numEventsReceived).to.equal(parseInt(numEventsExpected)),“Wrong number of events received”);

I know this works
if (parseInt(numEventsExpected) != parseInt(numEventsReceived)) {
pm.expect.fail(“wrong number of events received”)
}

but would rather have a pm.expect line…