Postman API testing: Unable to assert a value is true - postman

I am testing an API with a GET request that returns the following data:
{
"Verified": true,
"VerifiedDate": 2018-10-08
}
I am trying to test that the first field comes back true, and the second field has a value. I have the following code:
pm.test("Verified should be true", function () {
var Status = pm.response.json();
pm.expect(Status.Verified).to.be.true;
});
pm.test("Returns a verified date", function () {
var Status = pm.response.json();
pm.expect(Status.VerifiedDate).to.not.eql(null);
});
The assert on true is failing for the following reason:
Verified should be true | AssertionError: expected undefined to be true
Why is the first test failing?
I am running the same test on a post command without any issues.
Any ideas?
thanks

Root cause:
Your result is an array but your test is verifying an object. Thus, the postman will throw the exception since it could not compare.
Solution:
Use exactly value of an item in the list with if else command to compare.
var arr = pm.response.json();
console.log(arr.length)
for (var i = 0; i < arr.length; i++)
{
if(arr[i].Verified === true){
pm.test("Verified should be true", function () {
pm.expect(arr[i].Verified).to.be.true;
});
}
if(arr[i].Verified === false){
pm.test("Verified should be false", function () {
pm.expect(arr[i].Verified).to.be.false;
});
}
}
Hope it help you.

You could also just do this:
pm.test('Check the response body properties', () => {
_.each(pm.response.json(), (item) => {
pm.expect(item.Verified).to.be.true
pm.expect(item.VerifiedDate).to.be.a('string').and.match(/^\d{4}-\d{2}-\d{2}$/)
})
})
The check will do a few things for you, it will iterate over the whole array and check that the Verified property is true and also check that the VerifiedDate is a string and matches the YYYY-MM-DD format, like in the example given in your question.

Related

Postman = To assert that the array in the response body contains a string value in all its elements

I'm new to Postman. My Postman is failing to match test criteria. Could anyone please help! It is returning true even if there is no match
pm.test('Hourly metrics report generated to only 1 building', () => {
_.each(jsonData.attributes, (item) => {
pm.expect(item.building_ref_id).to.include('Mounting_View-EN-1-Internal_Alpha')
})
})
{
"id": "423317",
"type": "space",
"attributes": {
"name": "RM_05_030",
"space_ref_id": "RM_05_030",
"building_ref_id": "80_Fen",
"floor_ref_id": "5"
}
}
I assume that the code above is incomplete and that the response is actually and array of objects and that jsonData contains the parsed response body as a JavaScript array of objects.
The _.each() function takes an array as its first parameter, so you should not pass jsonData.attributes. This is the reason that your test passed; jsonData.attributes isn't an array, your callback will never get executed and no failing assertions means a succeeding test.
You can iterate over jsonData and assert that every item has a nested attribute item.attributes.building_ref_id containing your string:
jsonData = pm.response.json()
pm.test('Hourly metrics report generated to only 1 building', () => {
_.each(jsonData, (item) => {
pm.expect(item.attributes.building_ref_id).to.include('Mounting_View-EN-1-Internal_Alpha')
})
})

OR Condition in Postman assertion

Hi I have the following assertion in postman but it is failing
pm.test("Body matches string", function() {
pm.expect(pm.response.text()).to.include('Success')
|| pm.expect(pm.response.text()).to.include('Request already submitted');
});
My Response contains text success or Request already submitted. Please help.
pm.test("Body matches string", function () {
let a = "Success"
"Request already submitted" === a ? pm.expect("Request already submitted").to.be.equal('Request already submitted') : pm.expect(a).to.be.equal('Success')
});
pm.test("Body matches string", function () {
let a = "Success"
try {
pm.expect("Request already submitted").to.be.equal('Request already submitted')
}
catch (e) { pm.expect(a).to.be.equal('Success') }
});
pm.test("Body matches string", function () {
let a = "Success"
pm.expect(a).to.be.oneOf(['Success', 'Request already submitted']);
});
expect does not return a boolean it throws an error, so either catch it or use the oneof methed, or first check a condition and then assert

Postman test is always passing even though it fails

While running postman Tests, Test case seems to be always passing
The response body is provided below. I am trying to fetch id when the name is "Erin" and validate that id is 800. Small piece of code that i wrote is below the response body written below.FOr some reason the test always returns true. If for some reason if Erin and 800 are not present still it passes the test.
[
{
"id":991,
"name":"Tomy"
},
{
"id":800,
"name":"Erin"
}
]
Code:
pm.test("Validate id to be 800", function() {
var jsonData = pm.response.json();
for(int i=0; i<responseJson.length;i++){
if(jsonData[i].name=='Erin'){
pm.expect(jsonData[i].id).to.eql(800);
}
}
});
Updated the response a bit a below , i wanted my test to fail as "Jack" is
not found and to pass only if Jack is found
pm.test("Validate id to be 800", function () {
let jsonData = pm.response.json();
for(i=0; i < jsonData.length; i++) {
if(jsonData[i].name == 'Jack') {
pm.expect(jsonData[i].id).to.eql(800);
}
}
});
That response body doesn't look quite right to me, I would expect to see quotes around the property keys in the objects.
Also, your references were not named correctly and that would pass the test as it wouldn't have caused any reference errors in the scripts.
This should help you out:
pm.test("Validate id to be 800", function () {
let jsonData = pm.response.json();
for(i=0; i < jsonData.length; i++) {
if(jsonData[i].name === 'Erin') {
pm.expect(jsonData[i].id).to.eql(800);
}
}
});
You could rewrite the test code to something like this:
pm.test("Validate id to be 800", () => {
let jsonData = pm.response.json();
jsonData.forEach(item => {
if(item.name === 'Erin') {
pm.expect(item.id).to.eql(800);
}
});
});
And the Test Results when it fails:

get response from expo sqlite query

According to Expo documentation with SQLite I would make a query like so:
tx.executeSql(sqlStatement, arguments, success, error)
I execute it like this:
db.transaction(tx => {
tx.executeSql('SELECT * FROM dr_report_properties WHERE orderId = (?)', [this.state.orderId]);
},
error => {
alert(error);
},
(tx, results) => {
console.log(results);
}
);
My question is how do I get the response? The above returns as undefined.
I then try (not expecting it to work but just for kicks)
console.log(tx);
This does give a console.log
(tx, results) => {
console.log('I got data');
}
)
According to the documentation:
ResultSet objects are returned through second parameter of the success callback for the tx.executeSql() method on a Transaction (see above). They have the following form:
{
insertId,
rowsAffected,
rows: {
length,
item(),
_array,
},
}
I would expect result would be this object. Any ideas at what I'm doing wrong?
The problem with the above was that I placed the call AFTER the execution it's actually in the same method as that.
The result should have gone in the callback like so:
db.transaction(
tx => {
tx.executeSql('select * from my_table', [], (trans, result) => {
console.log(trans, result)
});
}
);
Thanks to #charliecruzan from expo team!

Postman API Tests

I have a response body like
{
"agreementId": "agreement900",
"Status": "ONHOLD"
}
The value of the status can be one of
['PAID','CANCELLED','COOLINGOFF','ONHOLD','COOLINGOFF','PAID']
I need to write a generic test to verify that the body.Status is always among the specified array.
I tried something like this
var data = ['PAID','CANCELLED','COOLINGOFF','ONHOLD','COOLINGOFF','PAID'];
pm.test("Verify Body value", function () {
let testResult = data.find((each)=>{
pm.expect(each.payoutStatus).to.equal(jsonData.payoutStatus)
});
});
But received the following error: Verify Body value | AssertionError: expected undefined to equal 'ONHOLD'
Deepak, welcome to SO
I am not sure about edge cases nor performance, but this can be a way of achieving it:
var myStatus = pm.response.json().Status;
var myEnum = ['Pig','Chicken','Cow'];
pm.test("Status belongs to the ENUMs", function () {
pm.expect(myEnum).to.include(myStatus);
});