Promise.resolve().然后在茉莉花测试中不起作用

Promise.resolve().then not working in jasmine test

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

var promise;

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

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

出于某种原因,当我 运行 执行此操作时,出现超时

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

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

干杯

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

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