【问题标题】:Promise.resolve().then not working in jasmine testPromise.resolve().then 在茉莉花测试中不起作用
【发布时间】:2016-09-28 11:54:14
【问题描述】:

我正在尝试设置一个涉及承诺的测试。这是我的示例代码:

var promise;

beforeEach(inject(function ($q) {
    promise = $q.resolve();
}));

it('should resolve', function (done) {
    promise.then(function () {
        expect(true).toBeTruthy();
        done();
    });
});

由于某种原因,当我运行它时,我得到一个 TIMEOUT

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

为什么 promise 不执行给 then 的回调?

干杯

【问题讨论】:

  • 没有必要对 promises 使用 done 方法。

标签: javascript angularjs unit-testing jasmine angular-promise


【解决方案1】:

你需要调用 scope/rootScope $digest 方法来解决 Promise。 所以应该是:

var result = false;
promise.then(function() { result = true;});
$rootScope.$digest();
expect(result).toBeTruthy();

【讨论】:

  • 你真的不应该调用 $digest 吗?
  • 如果有的话 $rootScope.$digest()。可能被 try/catch 包围。不过,对我也不起作用。
猜你喜欢
  • 2015-02-10
  • 2023-03-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多