I'm trying unit test a promise ["q": "^1.1.2"]; the following code works when the error is not being thrown. I need to validate the expected behaviour that the error is handled correctly.
Code:
Service.prototype.getItem = function (itemId) {
var deferred = Promise.defer();
// Ensure
if (typeof itemId !== 'string' || itemId === '') {
return deferred.reject(new Error('itemId is a required field'));
}
....
// do some logic at return item
return deferred.resolve(item);
....
return deferred.promise;
Since the method hasnt returned the promise, I cant test it for error handling! Is there a better way for the service to handle the promise but to return an error if one of the params are wrong.
Unit Test:
it('Call Service - should throw an error', function (done) {
Service.getItem('')
.then(function (item) {})
.catch(function (err) {
expect(err).not.to.be.empty();
done()
}).done();
});
Aucun commentaire:
Enregistrer un commentaire