1

when i am trying to test a function which returns a promise i get the following error:

"Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.

"

my spec is as follows:

describe('async promise test', function () {

    beforeEach(module('app'));

    beforeEach(function () {
         jasmine.DEFAULT_TIMEOUT_INTERVAL = 6 * 1000;
    })

    it('should match the name', function (done) {
        inject(function ($rootScope,promiseTest) {
          $rootScope.$apply(function(){
             var promise =promiseTest.checkPromise();
            promise.then(function(data){
           console.log(data);
           done();
          })
        })
      })
    })
});

please check the plunker link for the complete code plunker link

Soham Nakhare
  • 425
  • 4
  • 18

2 Answers2

0

You are using the mock module, which overrides $timeout, so that tests are repeatable (ref)!

You have to use $timeout.flush(1000); (and the $apply() is redundant):

it('should match the name', function (done) {
  inject(function (promiseTest,$timeout) {
    var promise =promiseTest.checkPromise();
    promise.then(function(data){
      console.log(data);
      done();
    });
    $timeout.flush(1000);
  });
});

See forked plunk: http://plnkr.co/edit/J1EmU7yuCETBd8w9mS1R?p=preview

Nikos Paraskevopoulos
  • 39,514
  • 12
  • 85
  • 90
0

You have to inject the $timeout into your test, and flush it:

 inject(function ($rootScope,promiseTest, $timeout) {
      $rootScope.$apply(function(){
         var promise =promiseTest.checkPromise();
         console.log("Created promise")
        promise.then(function(data){
       console.log(data);
       done();
      })
    });
    $timeout.flush();

See the modified plunker here:

http://plnkr.co/edit/psBl6nROkXHrD1iExKAQ?p=preview

Carl
  • 1,266
  • 10
  • 20
  • hey carl, actually i am trying to test indexed db, it is not working as expected.... link : http://plnkr.co/edit/dSdapvqTgR7qPeZC0umT?p=preview – Soham Nakhare Feb 11 '15 at 08:13