Assert over a dynamic boolean

I have a dynamic boolean in my code
i set it from the pre-request script

pm.collectionVariables.set(“explicitLyricsBoolean”, pm.collectionVariables.replaceIn(‘{{$randomBoolean}}’));

I wanted to test whether the correct boolean is displaying in the response, so i wrote the following test:-

var trackExplicitLyrics = pm.collectionVariables.get(“explicitLyricsBoolean”)

pm.test(“Explicit Lyrics field is correct boolean”, function (){

pm.expect(response.explicit).to.eql(trackExplicitLyrics)

})

However im getting the following error:
AssertionError: expected false to deeply equal ‘false’

what am i doing wrong?

As a starter for 10, write both variables to the console and check that they are the same, and that they are both strings.

console.log(response.explicit)
console.log(trackExplicitLyrics)

I suspect that one is a string (response.explicit) and one a Boolean (trackExplicitLyrics).

i got true
“true”
wouldnt that imply that trackExplicitLyrics is a string?
how do i fix this then?

You can test this to prove what type a variable is.

pm.test('myVariable is ??', () => {
    pm.expect(variableName.to.be.an('string');
    // pm.expect(variableName.to.be.an('boolean');
    // pm.expect(variableName.to.be.an('object');
    // pm.expect(variableName.to.be.an('array');
});

You can also just write this to the console.log

console.log(typeof(dataVariable))

The Tests tab in Postman uses JavaScript under the hood, so when you have these types of issues, you can just search Google.

@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

thank you @mdjones
im going to look over this

@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?

@atchyutpulavarthi
im getting an error
SyntaxError: Invalid or unexpected token

@hannahbroch Can you please share the code you’ve added to your test script?

@atchyutpulavarthi

var trackExplicitLyrics = pm.collectionVariables.get(“explicitLyricsBoolean”) == true

pm.test(“Explicit Lyrics field is correct boolean”, function () {
pm.expect(response.explicit).to.eql(trackExplicitLyrics);
});

@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);
});

@atchyutpulavarthi
i did as you said but im still getting this assertion error
expected true to deeply equal false

@hannahbroch
Consider the following…

var response ='true';
console.log(response); // 'true'

var trackExplicitLyrics = true;
console.log(trackExplicitLyrics); // true

var responseBoolean = response.toLowerCase() === 'true';
console.log(responseBoolean); // true

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 passes

image

@hannahbroch

False values are slightly different.

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

image