Compare Response with variable

Asked

Viewed 245 times

0

I made an environment variable that takes the ID of a registered Account, and when I go in my Find ID method I’m validating to see if I brought what I’m waiting for by passing code:8631 as Request. The return of my method shows the code being 8631. My environment variable "idConta" also has the value 8631. However this giving error, says that the number is not equal to the other, already tried with pm.expect also and nothing.

let responseJson = JSON.parse(responseBody);

pm.test("deve retornar um elemento",function(){  
    if(responseJson.code === "{{idConta}}"){  
        pm.response.to.be.ok;  
    }else{  
        pm.response.to.be.error;  
});
  • The returned data type is the same as you compare in the condition?

1 answer

0

If your return is as expected, it is very likely that the problem is in the data type returned/compared in the condition.

In the JavaScript, since the types of variables are not always taken into account, there are two options: the operator == (and the same goes for the operator !=), which does not take into account the type of data; and the operator === (and also !==), which considers the type of data, which makes it possible to affirm that values are, in fact, equal. Example:

var verdadeiro = '1' == 1;
var falso = '1' === 1;

For testing, you can check the data type with the function typeof.

console.log(typeof(responseJson.code));

So you can either change the type of return, make a casting in the condition itself, or simply change the condition to form that does not apply the type comparison.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.