Using collection variable in test

Hi,

I am having problems trying to use collection variable as an assertion in a test. I think my syntax is off.

Collection variables:

I try to get the variable with:

pm.collectionVariables.get(“createdSegmentId”) //i’m not sure if it should be with or without quotes

I try to use it later with:

pm.expect((jsonData.data.createLp.segmentId)).to.equal(createdSegmentId) // i’ve tried with and without {{}} around variable name

In the end I get an error:

ReferenceError: createdSegmentId is not defined

thanks for any help

1 Like

Hi @timothy.jahn! Welcome to the Postman forum and community! :trophy:

The issue here is that you have used the pm.collectionVariables.get method, but you didn’t actually retrieve that value and assign it to a local variable. So in line 16, there’s no local variable actually called createdSegmentId. If you change line 14 to say the following, it should work:

let createdSegmentId = pm.collectionVariables.get("createdSegmentId");

Another option would be to avoid setting a local variable. To do that, you can retrieve the value of the collection variable directly in line 16 with the following:

pm.expect((jsonData.data.createLp.segmentId).to.equal(pm.collectionVariables.get("createdSegmentId")));

Hope that helps!

2 Likes