角度模块在Karma Jasmine测试运行中不可用

时间:2014-02-11 11:29:18

标签: javascript angularjs jasmine karma-runner

我正在使用angular full stack进行开发,我的karma.conf.js文件是

files: [             
    'app/bower_components/jquery/jquery.js',
    'app/bower_components/angular/angular.js',
    'app/bower_components/angular-mocks/angular-mocks.js',
    'app/bower_components/angular-cookies/angular-cookies.js',
    'app/bower_components/angular-resource/angular-resource.js',
    'app/bower_components/angular-route/angular-route.js',
    'app/bower_components/angular-sanitize/angular-sanitize.js',
    'app/bower_components/angular-scenario/angular-scenario.js',
    'app/scripts/controllers/*.js',
    'app/scripts/directives/*.js',
    'app/scripts/services/*.js',
    'app/scripts/app.js',
    'lib/routes.js',           
    'test/karma/unit/**/test.spec.js'      
],

测试规范:

'use strict';

(function() {
describe('App', function() {

    describe('TestController', function() {

        beforeEach(function() {
            this.addMatchers({
                toEqualData: function(expected) {
                    return angular.equals(this.actual, expected);
                }
            });
        });

        // Load the controllers module
        beforeEach(module('ratefastApp'));

        // Initialize the controller and a mock scope
        var TestController,
            mockUserResource,
            scope,
            $httpBackend,
            $routeParams,
            $location;

        // The injector ignores leading and trailing underscores here (i.e. _$httpBackend_).
        // This allows us to inject a service but then attach it to a variable
        // with the same name as the service.

        beforeEach(
            inject(function($controller, $rootScope, _$location_, _$routeParams_, _$httpBackend_) {
            scope = $rootScope.$new();
            TestController = $controller('TestController', {
                $scope: scope
            });
            $routeParams = _$routeParams_;
            $httpBackend = _$httpBackend_;
            $httpBackend.when('GET', '/api/test/page/:pagenum')
                .respond([{title: 'test'}]);
            $location = _$location_;

        }));
    });
});
});

在运行上面的时候我得到了$injector:nomod Module is not available

2 个答案:

答案 0 :(得分:47)

在应用程序的其余部分之前,需要将模块加载到您的karma文件中。

这是因为“在尚未定义模块时调用不带依赖关系数组的angular.module会导致抛出此错误”docs.angularjs.org。因此,您必须在应用程序的其余部分之前显式加载文件。

在您的Karma.config javascript文件中:

    'app/bower_components/jquery/jquery.js',
    'app/bower_components/angular/angular.js',
    'app/bower_components/angular-mocks/angular-mocks.js',
    'app/bower_components/angular-cookies/angular-cookies.js',
    'app/bower_components/angular-resource/angular-resource.js',
    'app/bower_components/angular-route/angular-route.js',
    'app/bower_components/angular-sanitize/angular-sanitize.js',
    'app/bower_components/angular-scenario/angular-scenario.js',

    'app/scripts/app.js',        // Load your module before the rest of your app.

    'app/scripts/controllers/*.js',
    'app/scripts/directives/*.js',
    'app/scripts/services/*.js',
    'lib/routes.js',           
    'test/karma/unit/**/test.spec.js' 

答案 1 :(得分:0)

此错误表示找不到某个模块。具体来说,source for loader.js似乎表明当您没有使用angular.module注册模块时会抛出此错误。您是否使用ratefastApp完成了此操作?这是复制的来源:

    if (!requires) {
      throw $injectorMinErr('nomod', "Module '{0}' 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.", name);
    }  

此外,由于您尝试使用模拟$inject注入$controller, $rootScope, _$location_, _$routeParams_, _$httpBackend_,因此我首先要确保您的karma.conf.js中包含这些服务的文件{{1}指令。您可能还希望使用通配符来包含所有角度文件。