@hannahbroch the trackExplicitLyrics is a string since variables in postman are always stored as string values. In this case, you could compare the string value retrieved from the collectionVariables against a true or false using a double equals == , double equals will perform a type coercion from string to boolean and will check for equality, that check will return your actual true/false value and then you can compare it with the response.explicit in your test. You can achieve this like so:
var trackExplicitLyrics = pm.collectionVariables.get("explicitLyricsBoolean") == true; // This will return a true or false value that would be the actual value of the variable
pm.test("Explicit Lyrics field is correct boolean", function () {
pm.expect(response.explicit).to.eql(trackExplicitLyrics);
});
How this works?
pm.collectionVariables.get(“explicitLyricsBoolean”) == true;
/*
If pm.collectionVariables.get(“explicitLyricsBoolean”) returns a string 'true' then this comparison would be 'true' == true, and 'true' is equal to true due to the type coercion performed by == operator.
Instead if pm.collectionVariables.get("explicitLyricsBoolean") returns a string 'false'
then this comparison would be 'false' == true which is ultimately false even after
the type coercion. So in the end you'll be receiving the actual true or false value
of your collection variable from that comparison.
Then you can compare it to the value you retrieved from response.explicit
*/
Feel free to respond in case you have any other queries.
thank you @atchyutpulavarthi
i tried this, the problem is if the boolean in the body is false, i get the following error message
AssertionError: expected false to deeply equal true
if the boolean in the body is true then the test passes correctly
@hannahbroch that is my bad, I just remembered that the Boolean constructor actually treats a “false” boolean value as true. You could instead use a == operator to perform type coercion and then compare the actual values. I have updated the code snippet in my previous response, could you please try that?
@hannahbroch this is happening because of the curly quotation marks, it is possible that in case you copied and pasted that code block, the OS pasted these encoded curly quotes “ instead of normal double quotes ". Compilers and interpreters do not understand the curly quotes and you will need to either replace them with single quotes ' or normal double quotes ".
From this:
var trackExplicitLyrics = pm.collectionVariables.get(“explicitLyricsBoolean”) == true
pm.test(“Explicit Lyrics field is correct boolean”, function () {
pm.expect(response.explicit).to.eql(trackExplicitLyrics);
});
To this:
var trackExplicitLyrics = pm.collectionVariables.get('explicitLyricsBoolean') == true
pm.test('Explicit Lyrics field is correct boolean', function () {
pm.expect(response.explicit).to.eql(trackExplicitLyrics);
});
var response ='false';
console.log(response); // 'false'
var trackExplicitLyrics = false;
console.log(trackExplicitLyrics); // false
var responseBoolean = response.toLowerCase() === 'false';
console.log(responseBoolean); // true
var falseValue = JSON.parse(response); // this will create a Boolean object
console.log(falseValue); // false
pm.test('Explicit Lyrics field is correct boolean', () => {
pm.expect(response).to.eql(trackExplicitLyrics);
}); // this will fail
pm.test('Explicit Lyrics field is correct boolean', () => {
pm.expect(responseBoolean).to.eql(trackExplicitLyrics);
}); // this will also fail
pm.test('Explicit Lyrics field is correct boolean', () => {
pm.expect(falseValue).to.eql(trackExplicitLyrics);
}); // this passes