# Assert over a dynamic boolean

**URL:** <https://community.postman.com/t/assert-over-a-dynamic-boolean/39137>\
**Category:** Help Hub\
**Created:** [September 15, 2022, 6:30am UTC](https://community.postman.com/t/assert-over-a-dynamic-boolean/39137 "2022-09-15T06:30:08Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![hannahbroch](https://avatars.discourse-cdn.com/v4/letter/h/ecae2f/32.png) [@hannahbroch](https://community.postman.com/u/hannahbroch)\
**Post date:** [September 15, 2022, 6:30am UTC](https://community.postman.com/t/assert-over-a-dynamic-boolean/39137/1 "2022-09-15T06:30:08Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [September 15, 2022, 11:32am UTC](https://community.postman.com/t/assert-over-a-dynamic-boolean/39137/2 "2022-09-15T11:32:12Z")

</div>

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

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

```

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

---

<div class="post-metadata">

**Author:** ![hannahbroch](https://avatars.discourse-cdn.com/v4/letter/h/ecae2f/32.png) [@hannahbroch](https://community.postman.com/u/hannahbroch)\
**Post date:** [September 16, 2022, 5:37am UTC](https://community.postman.com/t/assert-over-a-dynamic-boolean/39137/3 "2022-09-16T05:37:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [September 16, 2022, 8:30am UTC](https://community.postman.com/t/assert-over-a-dynamic-boolean/39137/4 "2022-09-16T08:30:00Z")

</div>

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

```auto
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

```auto
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.

> **[JavaScript string to boolean | Convert](https://tutorial.eyehunts.com/js/javascript-string-to-boolean-convert/)**
>
> The easiest way to convert a string to a boolean is to compare the string with 'true' in JavaScript. Use toLowerCase() to convert a given...

---

<div class="post-metadata">

**Author:** ![atchyutpulavarthi](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/atchyutpulavarthi/32/28229_2.png) [@atchyutpulavarthi](https://community.postman.com/u/atchyutpulavarthi)\
**Post date:** [September 17, 2022, 1:28pm UTC](https://community.postman.com/t/assert-over-a-dynamic-boolean/39137/5 "2022-09-17T13:28:11Z")

</div>

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

```auto
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?**

```auto
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.

---

<div class="post-metadata">

**Author:** ![hannahbroch](https://avatars.discourse-cdn.com/v4/letter/h/ecae2f/32.png) [@hannahbroch](https://community.postman.com/u/hannahbroch)\
**Post date:** [September 17, 2022, 6:09pm UTC](https://community.postman.com/t/assert-over-a-dynamic-boolean/39137/6 "2022-09-17T18:09:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![hannahbroch](https://avatars.discourse-cdn.com/v4/letter/h/ecae2f/32.png) [@hannahbroch](https://community.postman.com/u/hannahbroch)\
**Post date:** [September 17, 2022, 6:12pm UTC](https://community.postman.com/t/assert-over-a-dynamic-boolean/39137/7 "2022-09-17T18:12:09Z")

</div>

thank you @michaelderekjones  
im going to look over this

---

<div class="post-metadata">

**Author:** ![atchyutpulavarthi](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/atchyutpulavarthi/32/28229_2.png) [@atchyutpulavarthi](https://community.postman.com/u/atchyutpulavarthi)\
**Post date:** [September 17, 2022, 6:40pm UTC](https://community.postman.com/t/assert-over-a-dynamic-boolean/39137/8 "2022-09-17T18:40:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![hannahbroch](https://avatars.discourse-cdn.com/v4/letter/h/ecae2f/32.png) [@hannahbroch](https://community.postman.com/u/hannahbroch)\
**Post date:** [September 17, 2022, 7:25pm UTC](https://community.postman.com/t/assert-over-a-dynamic-boolean/39137/9 "2022-09-17T19:25:22Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![atchyutpulavarthi](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/atchyutpulavarthi/32/28229_2.png) [@atchyutpulavarthi](https://community.postman.com/u/atchyutpulavarthi)\
**Post date:** [September 17, 2022, 7:36pm UTC](https://community.postman.com/t/assert-over-a-dynamic-boolean/39137/10 "2022-09-17T19:36:46Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![hannahbroch](https://avatars.discourse-cdn.com/v4/letter/h/ecae2f/32.png) [@hannahbroch](https://community.postman.com/u/hannahbroch)\
**Post date:** [September 18, 2022, 5:48am UTC](https://community.postman.com/t/assert-over-a-dynamic-boolean/39137/11 "2022-09-18T05:48:49Z")

</div>

@atchyutpulavarthi

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

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

---

<div class="post-metadata">

**Author:** ![atchyutpulavarthi](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/atchyutpulavarthi/32/28229_2.png) [@atchyutpulavarthi](https://community.postman.com/u/atchyutpulavarthi)\
**Post date:** [September 18, 2022, 6:00am UTC](https://community.postman.com/t/assert-over-a-dynamic-boolean/39137/12 "2022-09-18T06:00:37Z")

</div>

@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:**

```auto
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:**

```auto
var trackExplicitLyrics = pm.collectionVariables.get('explicitLyricsBoolean') == true

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

```

---

<div class="post-metadata">

**Author:** ![hannahbroch](https://avatars.discourse-cdn.com/v4/letter/h/ecae2f/32.png) [@hannahbroch](https://community.postman.com/u/hannahbroch)\
**Post date:** [September 20, 2022, 8:54am UTC](https://community.postman.com/t/assert-over-a-dynamic-boolean/39137/13 "2022-09-20T08:54:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [September 20, 2022, 12:51pm UTC](https://community.postman.com/t/assert-over-a-dynamic-boolean/39137/14 "2022-09-20T12:51:41Z")

</div>

@hannahbroch  
Consider the following…

```auto
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](https://us1.discourse-cdn.com/getpostman/original/2X/e/ea556ff0c289db9b9b5af9d11927f56255641bdc.png)

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [September 20, 2022, 1:04pm UTC](https://community.postman.com/t/assert-over-a-dynamic-boolean/39137/15 "2022-09-20T13:04:44Z")

</div>

@hannahbroch

False values are slightly different.

```auto
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](https://us1.discourse-cdn.com/getpostman/original/3X/c/0/c02c40cbb333820b0911c2b3c2a2ae99d2030fdb.png)
