Jhanz
Jhanz

Reputation: 157

how to write a test to match for data in array within postman test

I have created a variable called values, this value contains an array below:

var values = ["A","B", "C","D","E","F"]

I want to write a test to match json response data to one of the values in values.

var jsonData = pm.response.json(); 
pm.test("risk check", function () { 
pm.expect(jsonData.result.value).is.to.equal(values);  
});    

The data in response result.value can only be A, B, C, D, E, F

{
   "result":{
      "score":{
         "value":"F"
      }
   }
} 

Upvotes: 1

Views: 1296

Answers (1)

Danny Dainton
Danny Dainton

Reputation: 25881

You can use the oneOf method from the Chaijs library:

var jsonData = pm.response.json(); 
pm.test("risk check", function () { 
    pm.expect(jsonData.result.score.value).to.be.oneOf(values);  
});

This should then check to the values in the array against the response from the endpoint and will fail if it doesn't match.

Upvotes: 2

Related Questions