2016-03-08 86 views
0

我正在使用Karma和Jasmine進行單元測試。但是,我無法在單元測試中嘲笑或獲取模塊。我不斷收到同樣的錯誤使用ng-mock在角度單元測試中無法訪問模塊

Error: [$injector:modulerr] Failed to instantiate module ha.module.core due to: 
Error: [$injector:nomod] Module 'duScroll' 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. 

不過,我在上面

angular.mock.module('duScroll'); 
    angular.mock.module('ui.router'); 
    angular.mock.module('ha.module.utility'); 
    angular.mock.module('ha.module.core'); 

注入在beforeEach功能模塊在我karma.config文件,我需要的js文件

files: [ 
    '../node_modules/jquery/dist/jquery.js', 
    '../node_modules/angular/angular.js', 
    '../node_modules/angular-mocks/angular-mocks.js', 
    'src/modules/utility/module.utility.built.js', 
    'src/modules/utility/services/*.js', 
    'src/modules/core/module.core.built.js', 
    'src/modules/**/**/*.js', 
    'src/modules/core/**/*.js', 
    '../Templates/**/*.html', 
    'tests2/**/*.js', 
], 

我附上了一個屏幕截圖,顯示當我運行業力時顯示的內容。我正在將這些文件顯示在我的開發人員工具中。

enter image description here

我只能懷疑是是否有其他附加的東西,我必須做的就是啓動運行測試模塊?這些文件在angular和angular mocks之後加載,並且在我的測試文件夾之前加載。不知道爲什麼我不能獲取模塊。

回答

0

您不需要撥打angular.mock.module,對於您想要模擬的每個模塊而言,撥打beforeAll(module('<module-name>'))就足夠了。

然後,事後注入你的控制器,你必須使用

beforeEach(inject(function(_$controller_){ 
    // The injector unwraps the underscores (_) from around the parameter names when matching 
    $controller = _$controller_; 
})); 

Inforation從https://docs.angularjs.org/guide/unit-testing#testing-a-controller。請訪問它獲取更多信息。