如何在茉莉花上测试这个异步功能

时间:2017-10-24 20:16:21

标签: javascript angular asynchronous jasmine

我在服务中的功能是

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

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

          }, 2000);

          return deferred.promise;
}

在茉莉花测试中我有

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();
  });


});

我收到以下错误:

错误:超时 - 在jasmine.DEFAULT_TIMEOUT_INTERVAL指定的超时时间内未调用异步回调。

PhantomJS 2.1.1(Linux 0.0.0)testeando本地存储应支持异步执行测试准备和期望失败         预计未定义为真。

1 个答案:

答案 0 :(得分:0)

尝试将该测试的延迟参数设置为大于2000毫秒:

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

您也可以尝试为整个规范执行此操作:

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