2

I got a problem when testing my angular service with jasmine 2.0

I have a method in my service, returning a promise.

Now I would like to test, that this method returns or rejects this promise.

I smallered it down to this simple test:

describe('calling a promise returning function', function() {

        it('should resolve', function(done) {inject(function($q){
            var a;
            function call_me() {
                a = $q.defer();
                a.resolve();
                return a.promise;
            }

            call_me().then(
                function() {
                    console.log('yes');
                    expect(true).toBe(true);
                },
                function() {
                    console.log('nope');
                    expect(true).toBe(true);
                }
            ).finally(done);
        })});
});

But here I get the following return Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL. (1)

Slue
  • 311
  • 1
  • 4
  • I think you need to call `$rootScope.digest()` to trigger the resolution of the deferred. Do it, of course, after the `call_me(...)` block. – Nikos Paraskevopoulos May 23 '14 at 07:38
  • I'm closing as a broader duplicate rather than an exact one, that duplicate explains why this happens, and mentions how to fix it. I think you should consider reading it. – Benjamin Gruenbaum May 23 '14 at 08:05
  • 1
    @BenjaminGruenbaum Thank you, definitely good to know when working with $q – Slue May 23 '14 at 08:15

0 Answers0