简体   繁体   中英

how can I test this async function over jasmine

my function in the service is

listo: function () {
          var deferred = $q.defer();

          setTimeout(function () {
            deferred.resolve(true);

          }, 2000);

          return deferred.promise;
}

in jasmine test I have

describe("testeando local storage", function () {

  var bd;

  beforeEach(module('modulo'));

  beforeEach(inject(function (bds) {
    bd = bds;
  }));

  beforeEach(function () {
    expect(bd).toBeDefined();
  });

  var valor;

  it("prueba",function(done){
    bd.listo().then(function(res) {
      valor = res;
      done();
    })
  });

  it("pruebita", function(done) {
    expect(valor).toBe(true);
    done();
  });


});

I got the following error:

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

PhantomJS 2.1.1 (Linux 0.0.0) testeando local storage should support async execution of test preparation and expectations FAILED Expected undefined to be true.

Try to set delay parameter for that test to be more than 2000 ms:

it("prueba",function(done){
  bd.listo().then(function(res) {
    valor = res;
    done();
  })
}, 3000);

Also you may try to do it for whole the spec:

beforeEach(function() {
  jasmine.DEFAULT_TIMEOUT_INTERVAL = 3000;
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM