0

How do I test that an object retrieved from a fulfilled promise has a non-null property?

I have a promise that returns an object similar to the following:

{
    id: 1,
    aDate: 2015-02-12T11:50:00.511Z
}

I need to check that the aDate property is set and non null. How do I check for the existence of the aDate property without knowing its exact value (preferably using .should.eventually. ...?

Kallja
  • 5,362
  • 3
  • 23
  • 33

2 Answers2

1

Add more assertion to the property with and

promise.should.eventually.have.property('aDate').and.not.to.be.null;
eenagy
  • 912
  • 1
  • 8
  • 22
  • Thank you! That does exactly what I wanted. I guess that was just too intuitive for me to realize to try. :) – Kallja Feb 13 '15 at 06:18
0
return promise.then(function(obj) {
   assert(obj.aDate != null);
});
Esailija
  • 138,174
  • 23
  • 272
  • 326
  • I'd prefer to use an approach more 'chai-as-promised -like' so that I don't have to actually interact with the promise myself (as in promise.should.eventually.have.property('aDate'), which incidentally doesn't account for the non-nullness). – Kallja Feb 12 '15 at 13:21