Search code examples
postmanchaiassert

Comparing string value to .json data for API test in Postman


pm.test("Correct Asset Returned", 
function () {
var jsonData = pm.response.json();
pm.expect(jsonData.AssetId).to.equal(pm.variables.get("AssetNumber"));});

I get this error. (The test should pass) :

AssertionError: expected 315 to equal '315'**


Solution

  • It because the type of the AssetNumber variable is the String and the type of the AssetId is the Number. So you should convert to string or to number one of them before verify it.

    pm.expect(`${jsonData.AssetId}`).to.equal(pm.variables.get("AssetNumber"));});
    

    or

    pm.expect(jsonData.AssetId).to.equal(Number(pm.variables.get("AssetNumber")));});