Angular module not available in Karma Jasmine test run

匿名 (未验证) 提交于 2019-12-03 02:00:02

问题:

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

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.

回答1:

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

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.

In your Karma.config javascript 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/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:

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:

    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.



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!