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

时间:2022-08-24 22:30:08

I'm using angular full stack for development, my karma.conf.js file is

我正在使用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'      
],

Test Spec:

测试规格:

'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_;

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

On running the above I'm getting $injector:nomod Module is not available.

在运行上面我得到$ injector:nomod模块不可用。

2 个解决方案

#1


47  

The module(s) needs to be loaded in your karma files before the rest of the application.

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

This is because "Calling angular.module without the array of dependencies when the module has not yet been defined causes this error to be thrown" docs.angularjs.org. Therefore, you must explicitly load the files before the rest of your application.

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

In your Karma.config javascript files:

在您的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' 

#2


0  

This error indicates that some module is not being found. Specifically the source for loader.js seems to show that this error gets thrown when you haven't registered a module with angular.module. Have you done so with ratefastApp? Here's the copied source:

此错误表示未找到某个模块。具体来说,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);
    }  

Also, Since you are trying to inject $controller, $rootScope, _$location_, _$routeParams_, _$httpBackend_, with the mock $inject, I'd start by ensuring you have the files containing those services in your karma.conf.js files directive. You may also want to use wildcards to include all angular files.

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

#1


47  

The module(s) needs to be loaded in your karma files before the rest of the application.

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

This is because "Calling angular.module without the array of dependencies when the module has not yet been defined causes this error to be thrown" docs.angularjs.org. Therefore, you must explicitly load the files before the rest of your application.

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

In your Karma.config javascript files:

在您的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' 

#2


0  

This error indicates that some module is not being found. Specifically the source for loader.js seems to show that this error gets thrown when you haven't registered a module with angular.module. Have you done so with ratefastApp? Here's the copied source:

此错误表示未找到某个模块。具体来说,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);
    }  

Also, Since you are trying to inject $controller, $rootScope, _$location_, _$routeParams_, _$httpBackend_, with the mock $inject, I'd start by ensuring you have the files containing those services in your karma.conf.js files directive. You may also want to use wildcards to include all angular files.

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