# How to assert a response against a stored variable

**URL:** <https://community.postman.com/t/how-to-assert-a-response-against-a-stored-variable/10105>\
**Category:** Help Hub\
**Tags:** tests, scripts, response\
**Created:** [January 15, 2020, 4:48pm UTC](https://community.postman.com/t/how-to-assert-a-response-against-a-stored-variable/10105 "2020-01-15T16:48:23Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![carterusm69](https://avatars.discourse-cdn.com/v4/letter/c/49beb7/32.png) [@carterusm69](https://community.postman.com/u/carterusm69)\
**Post date:** [January 15, 2020, 4:48pm UTC](https://community.postman.com/t/how-to-assert-a-response-against-a-stored-variable/10105/1 "2020-01-15T16:48:23Z")

</div>

I’m new to Postman and looking for some help please.

I have stored a variable called ‘value’ from a previous request.

I have made a further request to a different API which also returns ‘value’.

I want to assert that the value returned in my latest call is the same as the value I stored as a variable.

I have created the following test:

pm.test(“Checking value sent in previous request is returned response”, function () {  
var jsonData = pm.response.json();  
pm.expect(jsonData.paymentStatus.amount.Value).to.equal(“{{value}}”);  
});

However, this test is failing with AssertionError: expected 11858.74 to equal ‘{{value}}’

Could anyone advise me how I can get this assertion to work ?

Thanks

---

<div class="post-metadata">

**Author:** ![allenheltondev](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/allenheltondev/32/31738_2.png) [@allenheltondev](https://community.postman.com/u/allenheltondev)\
**Post date:** [January 15, 2020, 5:14pm UTC](https://community.postman.com/t/how-to-assert-a-response-against-a-stored-variable/10105/2 "2020-01-15T17:14:09Z")

</div>

You need to load the variable when you’re building your javascript tests.

```javascript
pm.test(“Checking value sent in previous request is returned response”, function () {
  var jsonData = pm.response.json();
  let initialValue = pm.variables.get('value');
  pm.expect(jsonData.paymentStatus.amount.Value).to.equal(initialValue);
});

```

---

<div class="post-metadata">

**Author:** ![carterusm69](https://avatars.discourse-cdn.com/v4/letter/c/49beb7/32.png) [@carterusm69](https://community.postman.com/u/carterusm69)\
**Post date:** [January 16, 2020, 9:00am UTC](https://community.postman.com/t/how-to-assert-a-response-against-a-stored-variable/10105/3 "2020-01-16T09:00:10Z")

</div>

Thanks Allen.

When I copy and paste your response it returns the following warning : Expected ‘)’ and instead saw ‘value’. Missing “;” before statement. Any thoughts how to fix this ?

I’m still new to all of this so thanks for your patience.

Cheers

---

<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:** [January 16, 2020, 9:09am UTC](https://community.postman.com/t/how-to-assert-a-response-against-a-stored-variable/10105/4 "2020-01-16T09:09:37Z")

</div>

Hey @carterusm69,

That _error_ would have been a formatting issue from pasting it into your app. Discourse seems to not handle the quote marks very well, when pasting the code block into another app.

Just replace the two quote marks, at either end of the test name with new ones. That should remove the error you’re seeing.

---

<div class="post-metadata">

**Author:** ![carterusm69](https://avatars.discourse-cdn.com/v4/letter/c/49beb7/32.png) [@carterusm69](https://community.postman.com/u/carterusm69)\
**Post date:** [January 16, 2020, 9:35am UTC](https://community.postman.com/t/how-to-assert-a-response-against-a-stored-variable/10105/5 "2020-01-16T09:35:08Z")

</div>

Thanks Danny, that’s exactly whats happened. Now sorted. cheers 👍

---

<div class="post-metadata">

**Author:** ![chordy](https://avatars.discourse-cdn.com/v4/letter/c/e79b87/32.png) [@chordy](https://community.postman.com/u/chordy)\
**Post date:** [May 16, 2022, 11:35am UTC](https://community.postman.com/t/how-to-assert-a-response-against-a-stored-variable/10105/6 "2022-05-16T11:35:59Z")

</div>

![1](https://us1.discourse-cdn.com/getpostman/original/2X/1/11fe1e0d6396a50273287ad2bf3f0d4731caa135.png)  
Hi,  
I am getting this error can you please help me with what wrong I am doing here.

---

<div class="post-metadata">

**Author:** ![chordy](https://avatars.discourse-cdn.com/v4/letter/c/e79b87/32.png) [@chordy](https://community.postman.com/u/chordy)\
**Post date:** [May 16, 2022, 11:39am UTC](https://community.postman.com/t/how-to-assert-a-response-against-a-stored-variable/10105/7 "2022-05-16T11:39:56Z")

</div>

Got the answers, Solved the error by using the pm.collectionVariables.get

---

<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:** [August 23, 2023, 3:34pm UTC](https://community.postman.com/t/how-to-assert-a-response-against-a-stored-variable/10105/8 "2023-08-23T15:34:28Z")

</div>

Closed on August 23, 2023, 3:34pm UTC

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