Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
309 views
in Technique[技术] by (71.8m points)

javascript - Angular module not available in Karma Jasmine test run

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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' 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...