Javascript 角度模块在Karma Jasmine测试运行中不可用
我使用angular full stack进行开发,我的karma.conf.js文件是Javascript 角度模块在Karma Jasmine测试运行中不可用,javascript,angularjs,jasmine,karma-runner,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/angula
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模块不可用
此错误表示找不到某些模块。特别是,似乎表明,当您没有向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);
}
另外,由于您正试图使用mock注入$controller、$rootScope、\u$location、\u$routeParams、\u$httpBackend\uu
,因此我首先要确保您的karma.conf.js指令中包含这些服务的文件。您可能还希望使用通配符来包含所有角度文件。在应用程序的其余部分之前,需要将模块加载到您的karma文件中
这是因为“当尚未定义模块时,在没有依赖项数组的情况下调用angular.module会引发此错误”。因此,必须在应用程序的其余部分之前显式加载文件
在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'