错误:[$ injector:nomod]模块&#39; <name_of_module>&#39;不可用

时间:2016-02-01 16:55:52

标签: javascript angularjs jasmine karma-runner

这是我的配置文件karma.conf.js的一部分:

    // base path that will be used to resolve all patterns (eg. files, exclude)
basePath: '../',

// list of files / patterns to load in the browser
files: [
  'app/bower_components/angular/angular.js',
  'app/bower_components/angular-route/angular-route.js',
  'app/bower_components/angular-mocks/angular-mocks.js',
  'app/js/app.js',
  'app/js/controllers/*.js',
  'app/js/services/*.js',
  'test/unit/*.js'
],

这是我的文件夹struct:

folder's structure

这是app.js文件:

&#13;
&#13;
/**
 * Created by Pietro on 30/12/15.
 */
'use strict';

var drfmApp = angular.module('drfmApp', [
    'ngRoute','drfmControllers','ngCookies'
]);


drfmApp.config(['$routeProvider','$locationProvider',
    function($routeProvider) {
        $routeProvider.
        when('/', {
            templateUrl: 'views/login.html',
            controller: 'LoginController'
        }).
        when('/login', {
            templateUrl: 'views/login.html',
            controller: 'LoginController',
        }).
        when('/register', {
            templateUrl: 'views/register.html',
            controller: 'RegisterController',
        }).
        when('/dashboardESCO', {
            templateUrl: 'views/dashboard_esco.html',
            controller: 'DashboardESCOControllers'
        }).
        when('/dashboardRetailer', {
            templateUrl: 'views/dashboard_retailer.html',
            controller: 'DashboardRetailerControllers'
        }).
        when('/dashboardAggregator', {
            templateUrl: 'views/dashboard_aggregator.html',
            controller: 'DashboardAggregatorControllers'
        }).
        otherwise({
            redirectTo: '/login'
        });
    }]);

drfmApp.run(['$rootScope', '$location', '$cookieStore', '$http',
    function($rootScope, $location, $cookieStore, $http) {
        // keep user logged in after page refresh
        $rootScope.globals = $cookieStore.get('globals') || {};
        if ($rootScope.globals.currentUser) {
            $http.defaults.headers.common['Authorization'] = 'Basic ' + $rootScope.globals.currentUser.authdata; // jshint ignore:line
        }

        $rootScope.$on('$locationChangeStart', function (event, next, current) {
            // redirect to login page if not logged in and trying to access a restricted page

            // $ è jQuery
            var restrictedPage = $.inArray($location.path(), ['/login', '/register']) === -1;
            var loggedIn = $rootScope.globals.currentUser;
            if (restrictedPage && !loggedIn) {
                console.log('Run Config: User not logge in or trying to access a restricted web page')
                $location.path('/login');
            }
        });
    }]);
&#13;
&#13;
&#13;

我试图像这样运行一个简单的单元测试文件(使用Karma和Jasmine):

&#13;
&#13;
'use strict';

/* jasmine specs for controllers go here */
describe('Drfm controllers', function () {

    beforeEach(module('drfmControllers'));

    it('DUMMY TEST', function () {
        expect(3).toBe(3);
    });

});
&#13;
&#13;
&#13;

其中 drfmApp 是app.js文件中主模块的名称

这是定义drfmControllers的地方(在controllers.js中):

&#13;
&#13;
/**
 * Created by Pietro on 07/01/16.
 */
'use strict';

var drfmControllers = angular.module('drfmControllers', []);


drfmControllers.controller('DashboardESCOControllers', ['$scope',

    function($scope) {
        $scope.dashboard = "ESCO Dashboard";
    }

]);

drfmControllers.controller('DashboardRetailerControllers', ['$scope',

    function($scope){
        $scope.dashboard="Retailer Dashboard";}

]);

drfmControllers.controller('DashboardAggregatorControllers', ['$scope',

    function($scope){
        $scope.dashboard="Aggregator Dashboard";}

]);
&#13;
&#13;
&#13;

但我总是有这个错误:

    Error: [$injector:nomod] Module 'drfmControllers' 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.4.8/$injector/nomod?p0=drfmControllers at /Users/Pietro/repos/drfmcockpit/app/bower_components/angular/angular.js:68

我读到了karma配置文件中* .js文件的顺序:但似乎是根据这条规则[参见&#34;文件&#34;在问题的开头]。

感谢大家

更新解决方案:更改配置文件:

&#13;
&#13;
    // list of files / patterns to load in the browser
    files: [
      'app/bower_components/angular/angular.js',
      'app/bower_components/angular-route/angular-route.js',
      'app/bower_components/angular-mocks/angular-mocks.js',
      'app/js/app.js',
      'app/js/controllers/controllers.js',
      'app/js/controllers/LoginController.js',
      'app/js/controllers/RegisterController.js',
      'test/unit/*.js'
    ],
&#13;
&#13;
&#13;

运行良好。在文件检查中似乎有些东西。我不太清楚......

1 个答案:

答案 0 :(得分:1)

您尚未在配置选项中设置basePathBy default,等于''。反过来,空字符串作为basePath值,使其相对于配置文件目录。它被描述为here

  

如果basePath是相对路径,则会将其解析为目录   配置文件所在的位置

这意味着要么将basePath值设置为绝对路径,要么将文件路径更改为相对路径,例如:

files: [
  '../app/bower_components/angular/angular.js',
  '../app/bower_components/angular-route/angular-route.js',
  '../app/bower_components/angular-mocks/angular-mocks.js',
  '../app/js/app.js',
  '../app/js/controllers/*.js',
  '../app/js/services/*.js',
  'unit/*.js'
],