最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Angular JS Error: [$injector:nomod] Module 'portfolioMockupApp.services' is not available - Stack O

programmeradmin1浏览0评论

I'm attempting to write some unit tests with Karma and am receiving the following error:

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. .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.

Karma.conf files section:

  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):

'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);
  });
});

I'm attempting to write some unit tests with Karma and am receiving the following error:

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

Also, I have two separate files within the portfolioMockupApp.services module, both saved in the scripts/services directory.

Karma.conf files section:

  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):

'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);
  });
});
Share Improve this question edited Nov 25, 2014 at 19:11 Daniel Bogart asked Nov 25, 2014 at 18:47 Daniel BogartDaniel Bogart 1,2953 gold badges18 silver badges43 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 15

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.

  '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',

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/*.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.

'app/scripts/parentModule.js'
'app/scripts/*.js'
'app/scripts/anotherFolder/*.js'

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论