javascript Angular JS 错误:[$injector:nomod] 模块“portfolioMockupApp.services”不可用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27134375/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Angular JS Error: [$injector:nomod] Module 'portfolioMockupApp.services' is not available
提问by Daniel Bogart
I'm attempting to write some unit tests with Karma and am receiving the following error:
我正在尝试使用 Karma 编写一些单元测试,但收到以下错误:
PhantomJS 1.9.8 (Mac OS X) ERROR Error: [$injector:nomod] Module 'portfolioMockupApp.services' 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. http://errors.angularjs.org/1.3.3/$injector/nomod?p0=portfolioMockupApp.services at /Users/danielbogart/Documents/coding/work/portfolio-mockup/bower_components/angular/angular.js:1749
PhantomJS 1.9.8 (Mac OS X) ERROR 错误:[$injector:nomod] 模块“portfolioMockupApp.services”不可用!您拼错了模块名称或忘记加载它。如果注册模块,请确保将依赖项指定为第二个参数。 http://errors.angularjs.org/1.3.3/$injector/nomod?p0=portfolioMockupApp.services at /Users/danielbogart/Documents/coding/work/portfolio-mockup/bower_components/angular/angular.js:1749
Also, I have two separate files within the portfolioMockupApp.services module, both saved in the scripts/services directory.
此外,我在portfolioMockupApp.services 模块中有两个单独的文件,都保存在scripts/services 目录中。
Karma.conf files section:
Karma.conf 文件部分:
files: [
'bower_components/angular/angular.js',
'bower_components/angular-mocks/angular-mocks.js',
'bower_components/angular-animate/angular-animate.js',
'bower_components/angular-cookies/angular-cookies.js',
'bower_components/angular-resource/angular-resource.js',
'bower_components/angular-sanitize/angular-sanitize.js',
'bower_components/angular-touch/angular-touch.js',
'test/mock/**/*.js',
'test/spec/**/*.js',
'app/scripts/services/*.js',
'app/scripts/directives/*.js',
'app/scripts/controllers/*.js',
'app/scripts/app.js',
'node_modules/angular/angular.js',
'node_modules/angular-mocks/angular-mocks.js',
'./src/**/*.js',
'./test/**/*.js'
],
Portfolio.js spec (first and only test currently):
Portfolio.js 规范(目前第一个也是唯一一个测试):
'use strict';
describe('Controller: PortfolioCtrl', function () {
// load the controller's module
beforeEach(module('portfolioMockupApp', 'portfolioMockupApp.services'));
var PortfolioCtrl,
scope;
// Initialize the controller and a mock scope
beforeEach(inject(function ($controller, $scope, $log, $stateParams, $state,
$rootScope,portService, portfolioCreateService) {
scope = $rootScope.$new();
PortfolioCtrl = $controller('PortfolioCtrl', {
$scope: scope
});
}));
it('should have a list of 5 tabs by default', function () {
expect(scope.tabs.length).toBe(5);
});
});
回答by Daniel Bogart
The problem stemmed from having two separate service files using the same service module. In the Karma.conf file I had to explicitly load the service file that initialized the module, and then the other service file and rest of the app afterwards.
问题源于使用同一个服务模块的两个单独的服务文件。在 Karma.conf 文件中,我必须显式加载初始化模块的服务文件,然后是其他服务文件和应用程序的其余部分。
'app/scripts/services/port-service.js',
'app/scripts/services/new-port-service.js',
'app/scripts/app.js',
'app/scripts/services/*.js',
'app/scripts/directives/*.js',
'app/scripts/controllers/*.js',
回答by user1221844
Thanks for checking back in with a solution. I had this same issue when two modules relied on each other and existed in the same folder, lets call them app/scripts/parentModule.js and app/scripts/constants.js. Both should be picked up by the wildcard entry in karma.config.js.
感谢您提供解决方案。当两个模块相互依赖并存在于同一个文件夹中时,我遇到了同样的问题,我们称它们为 app/scripts/parentModule.js 和 app/scripts/constants.js。两者都应由 karma.config.js 中的通配符条目选取。
'app/scripts/*.js'
'app/scripts/anotherFolder/*.js'
Since constants.js relies on parentModule.js, the later must be included first and my guess is the wildcard was including the files alphabetically but I've not confirmed this yet.
由于constants.js 依赖于parentModule.js,后者必须首先包含,我猜是通配符是按字母顺序包含文件,但我还没有确认这一点。
'app/scripts/parentModule.js'
'app/scripts/*.js'
'app/scripts/anotherFolder/*.js'