silvixqa
(SilvixQA)
July 29, 2020, 12:10pm
1
Hi,
New to Postman and I would appreciate a little help.
I have the following response body and I would like to assert that “token_data” contains the access_token and refresh_token that both have strings.
{
“token_data”:{
“access_token”: “string”,
“refresh_token”: “string”
}
}
Help?
bpricilla
(Pricilla B)
August 31, 2020, 9:25am
2
Hello,
Yes it is possible.
You need to use type of.
Snippet below:
pm.test("Data Type is correct for access_token " , function(){
pm.expect(typeof(resp.token_data[0].access_token) === “string”).to.be.true;
});
pm.test("Data Type is correct for refresh_token " , function(){
pm.expect(typeof(resp.token_data[0].refresh_token) === “string”).to.be.true;
});
1 Like
Another way to write the tests in addition to how @bpricilla demonstrated would be below:
pm.test('Access token is a string', function(){
const jsonData = pm.response.json();
pm.expect(jsonData.token_data).to.have.property('access_token').and.to.be.a('string');
}
What’s nice about this method is that if you also want to test the value, you could change it to something like this:
pm.test('Access token has correct string', function(){
const jsonData = pm.response.json();
const expectedValue = 'my_access_token_expected_value';
pm.expect(jsonData.token_data).to.have.property('access_token', expectedValue);
}
3 Likes