如何解决Jasmine中的“ $ timeout不是函数”错误?

时间:2019-08-26 14:58:39

标签: javascript angularjs unit-testing jasmine karma-jasmine

我收到一个TypeError:this。$ timeout不是用Jasmine对angular.js应用进行单元测试的函数。

我在onInit上运行了一个超时服务,这给出了错误。

找不到解决此问题的解决方案。

onInit看起来像:

constructor($document, $window, $element, $timeout) {
    this.$document = $document;
    this.$window = $window;
    this.$element = $element;
    this.$timeout = $timeout;
}

$onInit() {
    const elementsList = this.element.children;
    const currentPage = elementsList.findIndex(pageController.findElement) + 1;
    const totalPages = elementsList.length;

    if (totalPages > 1) {
        this.elementsList = elementsList;
        this.currentPage = currentPage;
        this.totalPages = totalPages;
    }

    this.$timeout(() => {
        this.getCurrentPage().style.width = 'auto';
        this.currentStepWidth = this.getCurrentPage().clientWidth;
    });
}

和规格文件:

describe('$onInit', () => {
    fit('should set currentPage and totalPages', () => {
        let pageCounter = new pageController();
        pageCounter.element = {
            children: [
                { styles: ['pageCurrent'] },
                { styles: ['pageVisited'] }
            ]
        };

        // When
        pageCounter.$onInit();

        // Then
        expect(pageCounter.currentPage).toEqual(1);
        expect(pageCounter.totalSteps).toEqual(2);
    });
});

运行测试时,出现TypeError:this。$ timeout不是函数

我该如何解决?

0 个答案:

没有答案