Angular 模块在 Karma Jasmine 测试运行中不可用 [英] Angular module not available in Karma Jasmine test run

查看:35
本文介绍了Angular 模块在 Karma Jasmine 测试运行中不可用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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'      
],

测试规范:

'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.

推荐答案

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

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

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

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.

在您的 Karma.config javascript 文件中:

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' 

这篇关于Angular 模块在 Karma Jasmine 测试运行中不可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆