测试angular子模块无法找到依赖关系

时间:2016-12-29 08:47:45

标签: angularjs unit-testing testing angularjs-components

我正在尝试测试我的angular 1(使用typescript)模块,我遇到了依赖项问题:

这些是我的模块:

app.module.ts

export const appModule: IModule = angular.module('app', [
    'ui.router',
    'ngMaterial',
    'ngMessages',
    'ngAnimate',
    'angularMoment',
    'ngMap',
    sharedModule.name,
    componentsModule.name
]);

shared.module.ts

export const sharedModule: IModule = angular.module('app.shared', [
    'ui.router',
    'ngMaterial',
    'ngMessages',
    'ngAnimate',
    'angularMoment',
    'ngMap'
]);

但是在测试时我在所有我的测试中得到了错误,但是我的项目在浏览器中运行得很好。所以只有测试找不到像这样的依赖性抛出错误:

Uncaught Error: [$injector:modulerr] Failed to instantiate module app.shared due to:
Error: [$injector:modulerr] Failed to instantiate module ui.router due to:
Error: [$injector:nomod] Module 'ui.router' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.

但是当我将共享模块设置为:

export const sharedModule: IModule = angular.module('app.shared', []);

然后在使用外部服务的测试中出现 错误,例如:

Error: [$injector:unpr] Unknown provider: $stateProvider <- $state

测试如下:

describe('component: bookmark', () => {
    let parentScope: any;
    let element: any;

    beforeEach(angular.mock.module(sharedModule.name));

    beforeEach(inject(($compile: ng.ICompileService, $rootScope: ng.IRootScopeService) => {
        parentScope = $rootScope.$new();

        element = angular.element(`<bookmark></bookmark>`);
        $compile(element)(parentScope);

        parentScope.$digest();
    }));
    it('should display a star', () => {
        const someAttrValue: string = findIn(element, 'md-icon').text();
        console.debug('found ', someAttrValue);
        expect(someAttrValue).toEqual('star');

    });
});

我的bookmark需要$state

任何线索我可能做错了什么?

1 个答案:

答案 0 :(得分:0)

问题在于我没有导入所有依赖项

错误保持不变,因此当我导入ui-router时,它仍然表示在ui-router丢失angular-material时丢失了echo anchor('perkalian','Perkalian');