首页 > 解决方案 > 如何测试具有多个等待的 AngularJS 控制器方法 - Jasmine?

问题描述

我正在尝试async通过多个await调用来测试控制器中的方法,但似乎我遗漏了一些东西。

使用如下设置,await永远无法解决,不知道为什么。在控制台中,我看到第一个日志,第二个永远不会。当我更新要使用的方法then而不是await它时。

// Controller

async asyncMethod()
{
    console.log('############################################# 1');
    const var1 = await this.customService.getSettings();
    console.log('############################################# 2');

    // ...

    console.log('############################################# 3');
    const v2 = await this.customService.getSettings2();
    console.log('############################################# 4');

    // ...
}
// Test

customService.getSettings = () => { return $q.resolve(true); };
customService.getSettings2 = () => { return $q.resolve(true); };

describe('asyncMethod', function () {
    it('should call asyncMethod', function (done) {
        controller.asyncMethod();

        $rootScope.$digest();

        // expect statements
        // ...
    });
});

标签: angularjsjasmine

解决方案


推荐阅读