7

I'm working with a API project and using Postman for testing APIs. I wrote few test cases to check null as follows,

//user id is present
pm.test("user id is present", function() {
    pm.expect(jsonData.data[0].userId).not.eql(null);
});

Also tried with .not.equal() as described in this answer.

//user id is present
pm.test("user id is present", function() {
    pm.expect(jsonData.data[0].userId).not.equal(null);
});

However, this all are getting passed even if the userId is null. Any furhter thought to check null value in Postman test case?

Divyang Desai
  • 7,483
  • 13
  • 50
  • 76
  • `.to.not.be.null`? https://www.chaijs.com/api/bdd/#method_null But it always says in the docs that this is not recommend... – Danny Dainton Jan 28 '19 at 10:09
  • @DannyDainton: Tried `.to.not.be.null` however, no luck so far. Yes, I checked doc already, but then what is alternative? – Divyang Desai Jan 28 '19 at 10:18
  • Just trying to find something locally that works. Seems like nothing I've tried (each example I've come across) gives me the result I want. :( This feels like it should be so difficult...but it's proving to be. :( – Danny Dainton Jan 28 '19 at 10:21

1 Answers1

7

First of all, open the console and run the flowing snippet to check the types:

pm.test("user id is present", function() {
   console.log(typeof jsonData.data[0].userId);
   console.log(typeof jsonData.data[0]);
   console.log(typeof jsonData);
}

If you get undefined for jsonData.data[0].userId, you need change your test to assert null and undefined:

pm.test("user id is present", function() {
   pm.expect(jsonData.data[0].userId).to.exist //check if it is not equal to undefined
   pm.expect(jsonData.data[0].userId).to.not.be.null; //if it is not equal to null
}
Ângelo Polotto
  • 8,463
  • 2
  • 36
  • 37