2016-12-29 71 views
0

我想測試我的角度1(使用打字稿)模塊,並且我遇到的依賴性問題:測試角度子模塊找不到依賴

這是我的模塊:

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

任何線索我可能是做錯了什麼?

回答

0

的問題是,我並沒有進口所有的依賴,

誤差保持不變,所以當我進口ui-router,它仍然說這是缺少ui-router它缺少angular-material一段時間。