Test the angular service that returns a promise of $ q in Jasmine 2.0 asynchronously

advertisements

I have a spec which tests the result of a promise.

The promise resolves, but the then, catch, and finally handlers never seem to fire, and call done();

As a result, the spec times out and fails (see screen shot of Chrome Developer Console).

Jasmine suite and spec

describe("promise suite", function () {
        var someMethod = function() {
            var deferred = $q.defer();
            setTimeout(function() {
                console.log('All done calling resolve');
                deferred.resolve('all done');
            }, 500);
            return deferred.promise;
        }
        iit("promise spec", function (done) {
            someMethod()
                .then(function(message) {
                    console.log('promise method resolved, running expectation');
                    expect(message).toBe('all done');
                    done();
                })
                .catch(function(error) {
                    console.log('promise was rejected ', error);
                    done();
                })
                .finally(function() {
                    console.log('calling done from finally');
                    done();
                });
        });
    });


$q promises are resolved on the next digest... so you need to trigger a digest:

$rootScope.$digest();