# Body matches string fails

**URL:** <https://community.postman.com/t/body-matches-string-fails/15994>\
**Category:** Help Hub\
**Created:** [September 18, 2020, 8:20am UTC](https://community.postman.com/t/body-matches-string-fails/15994 "2020-09-18T08:20:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bgehrer](https://avatars.discourse-cdn.com/v4/letter/b/ac8455/32.png) [@bgehrer](https://community.postman.com/u/bgehrer)\
**Post date:** [September 18, 2020, 8:20am UTC](https://community.postman.com/t/body-matches-string-fails/15994/1 "2020-09-18T08:20:09Z")

</div>

Hi there

I do define variables taken from random variables in a pre-request script as following:

pm.collectionVariables.set(“username”, Math.floor(Math.random() \* 1000))  
pm.collectionVariables.set(“name”,"{{$randomLastName}}");

I use the same variables in my request body:

{  
“username”: “user{{username}}”,  
“name”: “{{name}}”,  
}

And then i test in my test script if i can find the values for {{username}} and {{name}} in the response:

pm.test(“Body matches string”, function () {  
pm.expect(pm.response.text()).to.include(“user”,"{{username}}");  
});

pm.test(“Body matches string”, function () {  
pm.expect(pm.response.text()).to.include("{{name}}");  
});

The first test succeds, but the second one fails…i do not see why as the value is correctly generated and also concluded in the response.

The console says:

Body matches string | AssertionError: expected ‘{“[name":“Stehr”,“prename”:“Nathaniel”,“username”:“user239”,“organisationalUnit”:“ABC-DEF-GHI”,“telephone”:“0581234567”,“email”:"Haleigh61@gmail.com](mailto:name%22:%22Stehr%22,%22prename%22:%22Nathaniel%22,%22username%22:%22user239%22,%22organisationalUnit%22:%22ABC-DEF-GHI%22,%22telephone%22:%220581234567%22,%22email%22:%22Haleigh61@gmail.com)”,“lang”:“DE”}’ to include ‘{{name}}’

Seems like the variable {{name}} is not loaded correctly?

Does anyone know of this problem?

Greetings and thanks for your help!

Benjamin

---

<div class="post-metadata">

**Author:** ![michal.robaszewski.j](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michal.robaszewski.j/32/5378_2.png) [@michal.robaszewski.j](https://community.postman.com/u/michal.robaszewski.j)\
**Post date:** [September 18, 2020, 9:13am UTC](https://community.postman.com/t/body-matches-string-fails/15994/2 "2020-09-18T09:13:36Z")

</div>

In Test tab you cannot use format like {{name}}  
you need to fetch variable and save it in JavaScript form

```auto
 pm.test("ABC visible in response", function() {
      const Name= pm.collectionVariables.get("name")
      pm.expect(pm.response.text()).to.include(Name) ;
    });

```

EDIT:  
Additional info about using .include assertions  
The first parameter is the value that you are searching for. The second one is msg wchich is optional in case of error so in test like

> [@bgehrer](#):
>
> pm.test(“Body matches string”, function () {  
> pm.expect(pm.response.text()).to.include(“user”,“{{username}}”);  
> });

You are searching for “user” only. Not value of variable {{username}}

Here are the official docs [Expect / Should - Chai](https://www.chaijs.com/api/bdd/#method_include)

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [September 18, 2020, 9:28am UTC](https://community.postman.com/t/body-matches-string-fails/15994/3 "2020-09-18T09:28:50Z")

</div>

You can access the variable using the `{{..}}` syntax in the sandbox by using:

```auto
pm.collectionVariables.replaceIn('{{name}}') 

```

---

<div class="post-metadata">

**Author:** ![michal.robaszewski.j](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michal.robaszewski.j/32/5378_2.png) [@michal.robaszewski.j](https://community.postman.com/u/michal.robaszewski.j)\
**Post date:** [September 18, 2020, 9:56am UTC](https://community.postman.com/t/body-matches-string-fails/15994/4 "2020-09-18T09:56:24Z")

</div>

As this dynamic variables also require additional line I like to stick with assigning it to named JS variable 😉  
But this is just my habit

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [September 18, 2020, 10:06am UTC](https://community.postman.com/t/body-matches-string-fails/15994/5 "2020-09-18T10:06:33Z")

</div>

Wasn’t pushing to use a certain method 😁 just responding to this 😁

> In Test tab you cannot use format like {{name}}
