# Assert over a string which contains a collection variable

**URL:** <https://community.postman.com/t/assert-over-a-string-which-contains-a-collection-variable/71614>\
**Category:** Help Hub\
**Tags:** tests, variables\
**Created:** [November 24, 2024, 7:21pm UTC](https://community.postman.com/t/assert-over-a-string-which-contains-a-collection-variable/71614 "2024-11-24T19:21:17Z")\
**Posts on this page:** 3\
**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:** [November 24, 2024, 7:21pm UTC](https://community.postman.com/t/assert-over-a-string-which-contains-a-collection-variable/71614/1 "2024-11-24T19:21:17Z")

</div>

I’d like to write a test to check whether my API returns a specific error message  
This error message however contains a collection variable  
When i run the test, im receiving a syntax error

Example:-

```auto
pm.test("Correct error message is returned", function (){
    pm.expect(response[0].errorMessage).to.include("Release level: Only use an artist name ((pm.collectionVariables.get("artistName1"))) in a title if it's really part of the title. Do NOT use it just to identify that track is by the artist.")
})

```

---

<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:** [November 25, 2024, 10:56am UTC](https://community.postman.com/t/assert-over-a-string-which-contains-a-collection-variable/71614/2 "2024-11-25T10:56:53Z")

</div>

You should be able to see in the editor that the code is incorrect before you send it and get the syntax error.

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/6/1/6174b046a9475e4601166b9a3711e6ba446f86a2.png)

You can build up the string using the following method.

```auto
"Release level: Only use an artist name "+ pm.collectionVariables.get("artistName1") +" in a title if it's really part of the title. Do NOT use it just to identify that track is by the artist."

```

You can also define the expected result in a variable to help with legibility.

```auto
pm.test("Correct error message is returned", function (){
    let actualResult = response[0].errorMessage
    let expectedResult = "Release level: Only use an artist name "+ pm.collectionVariables.get("artistName1") +" in a title if it's really part of the title. Do NOT use it just to identify that track is by the artist."
    pm.expect(actualResult).to.include(expectedResult);
})

```

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/system/32/29796_2.png) [@system](https://community.postman.com/u/system)\
**Post date:** [December 25, 2024, 10:57am UTC](https://community.postman.com/t/assert-over-a-string-which-contains-a-collection-variable/71614/3 "2024-12-25T10:57:30Z")

</div>

Closed on December 25, 2024, 10:57am UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
