Javascript 角业力测试运行不正常,出现错误“;参数';fn';不是函数,未定义;
我试图使用karma将单元测试添加到angular控制器中,但我遇到了一个问题,需要一些帮助。我在这里发现了类似的问题,但没有一个能让我找到答案。我试着改变karma.conf.js中“files”数组的顺序,认为它们可能没有被正确加载,但这并没有产生任何结果。当我运行'karma start karma.conf.js'时,我得到了错误 来自karma的控制台错误Javascript 角业力测试运行不正常,出现错误“;参数';fn';不是函数,未定义;,javascript,angularjs,jasmine,karma-runner,karma-jasmine,Javascript,Angularjs,Jasmine,Karma Runner,Karma Jasmine,我试图使用karma将单元测试添加到angular控制器中,但我遇到了一个问题,需要一些帮助。我在这里发现了类似的问题,但没有一个能让我找到答案。我试着改变karma.conf.js中“files”数组的顺序,认为它们可能没有被正确加载,但这并没有产生任何结果。当我运行'karma start karma.conf.js'时,我得到了错误 来自karma的控制台错误 Error: [$injector:modulerr] Failed to instantiate module undefine
Error: [$injector:modulerr] Failed to instantiate module undefined due to:
Error: [ng:areq] Argument 'fn' is not a function, got undefined
at assetArg (D:/xxxx/bower_components/angular/angular.js:1580
...
...
... (long callstack within angular.js and angular-mocks.js)
...
...
at workFn (D:/xxxx/bower_components/angular-mocks/angular-mocks.js:2172
我现在只使用一个测试文件,它非常基本。模块名称正确且“myApp.views.view1”文件由karma服务器提供,“调试”级别的karma日志记录确认了这一点
view1_test.js
'use strict';
describe('myApp.views.view1 module', function() {
beforeEach(module('myApp.views.view1',[]));
describe('view1 controller', function(){
it('should exist....', inject(function($controller) {
//spec body
var view1Ctrl = $controller('View1Ctrl');
expect(view1Ctrl).toBeDefined();
}));
});
});
'use strict';
angular.module('myApp.views.view1', ['ngRoute'])
.config(['$routeProvider', function($routeProvider) {
$routeProvider.when('/view1', {
templateUrl: 'src/views/view1/view1.html',
controller: 'View1Ctrl'
});
}])
.controller('View1Ctrl', ['$scope','$anchorScroll',function($scope,$anchorScroll) {
//scroll to top of page
$anchorScroll();
$scope.testVar = "it works, congrats!";
}]);
module.exports = function(config){
config.set({
basePath : './app/',
preprocessors: {
'src/directives/**/*.html': ['ng-html2js']
},
files : [
'src/bower_components/angular/angular.js',
'src/bower_components/angular-route/angular-route.js',
'src/bower_components/angular-mocks/angular-mocks.js',
'src/directives/**/*.js',
'src/views/**/*.js',
//directive templates
'app/src/directives/**/**.html'
],
autoWatch : true,
// level of logging
// possible values: LOG_DISABLE || LOG_ERROR || LOG_WARN || LOG_INFO || LOG_DEBUG
logLevel: config.LOG_DEBUG,
frameworks: ['jasmine'],
browsers : ['PhantomJS'],
plugins : [
'karma-chrome-launcher',
'karma-firefox-launcher',
'karma-phantomjs-launcher',
'karma-jasmine',
'karma-junit-reporter',
'karma-ng-html2js-preprocessor'
],
junitReporter : {
outputFile: '../test_out/unit.xml',
suite: 'unit'
},
ngHtml2JsPreprocessor: {
// strip app from the file path
stripPrefix: 'app/',
stripSufix: '.ext',
// prepend this to the
prependPrefix: 'served/',
// setting this option will create only a single module that contains templates
// from all the files, so you can load them all with module('foo')
moduleName: 'templates'
}
});
};
这是它正在测试的视图控制器
view1.js
'use strict';
describe('myApp.views.view1 module', function() {
beforeEach(module('myApp.views.view1',[]));
describe('view1 controller', function(){
it('should exist....', inject(function($controller) {
//spec body
var view1Ctrl = $controller('View1Ctrl');
expect(view1Ctrl).toBeDefined();
}));
});
});
'use strict';
angular.module('myApp.views.view1', ['ngRoute'])
.config(['$routeProvider', function($routeProvider) {
$routeProvider.when('/view1', {
templateUrl: 'src/views/view1/view1.html',
controller: 'View1Ctrl'
});
}])
.controller('View1Ctrl', ['$scope','$anchorScroll',function($scope,$anchorScroll) {
//scroll to top of page
$anchorScroll();
$scope.testVar = "it works, congrats!";
}]);
module.exports = function(config){
config.set({
basePath : './app/',
preprocessors: {
'src/directives/**/*.html': ['ng-html2js']
},
files : [
'src/bower_components/angular/angular.js',
'src/bower_components/angular-route/angular-route.js',
'src/bower_components/angular-mocks/angular-mocks.js',
'src/directives/**/*.js',
'src/views/**/*.js',
//directive templates
'app/src/directives/**/**.html'
],
autoWatch : true,
// level of logging
// possible values: LOG_DISABLE || LOG_ERROR || LOG_WARN || LOG_INFO || LOG_DEBUG
logLevel: config.LOG_DEBUG,
frameworks: ['jasmine'],
browsers : ['PhantomJS'],
plugins : [
'karma-chrome-launcher',
'karma-firefox-launcher',
'karma-phantomjs-launcher',
'karma-jasmine',
'karma-junit-reporter',
'karma-ng-html2js-preprocessor'
],
junitReporter : {
outputFile: '../test_out/unit.xml',
suite: 'unit'
},
ngHtml2JsPreprocessor: {
// strip app from the file path
stripPrefix: 'app/',
stripSufix: '.ext',
// prepend this to the
prependPrefix: 'served/',
// setting this option will create only a single module that contains templates
// from all the files, so you can load them all with module('foo')
moduleName: 'templates'
}
});
};
目录结构
/
.bowerrc
.gitignore
bower.json
package.json
karma.conf.js
/sass_styles
base.scss
/app
index.html
/src
app.js
/bower_components
/directives
/example
directive.js
template.html
/views
/view1
view1.html
view1.js
view1_test.js
/styles
base.css
这是我的karma.conf.js文件。我使用PhantomJS作为浏览器和html2js预处理器来包含使用“templateURL”的directive.html模板
karma.conf.js
'use strict';
describe('myApp.views.view1 module', function() {
beforeEach(module('myApp.views.view1',[]));
describe('view1 controller', function(){
it('should exist....', inject(function($controller) {
//spec body
var view1Ctrl = $controller('View1Ctrl');
expect(view1Ctrl).toBeDefined();
}));
});
});
'use strict';
angular.module('myApp.views.view1', ['ngRoute'])
.config(['$routeProvider', function($routeProvider) {
$routeProvider.when('/view1', {
templateUrl: 'src/views/view1/view1.html',
controller: 'View1Ctrl'
});
}])
.controller('View1Ctrl', ['$scope','$anchorScroll',function($scope,$anchorScroll) {
//scroll to top of page
$anchorScroll();
$scope.testVar = "it works, congrats!";
}]);
module.exports = function(config){
config.set({
basePath : './app/',
preprocessors: {
'src/directives/**/*.html': ['ng-html2js']
},
files : [
'src/bower_components/angular/angular.js',
'src/bower_components/angular-route/angular-route.js',
'src/bower_components/angular-mocks/angular-mocks.js',
'src/directives/**/*.js',
'src/views/**/*.js',
//directive templates
'app/src/directives/**/**.html'
],
autoWatch : true,
// level of logging
// possible values: LOG_DISABLE || LOG_ERROR || LOG_WARN || LOG_INFO || LOG_DEBUG
logLevel: config.LOG_DEBUG,
frameworks: ['jasmine'],
browsers : ['PhantomJS'],
plugins : [
'karma-chrome-launcher',
'karma-firefox-launcher',
'karma-phantomjs-launcher',
'karma-jasmine',
'karma-junit-reporter',
'karma-ng-html2js-preprocessor'
],
junitReporter : {
outputFile: '../test_out/unit.xml',
suite: 'unit'
},
ngHtml2JsPreprocessor: {
// strip app from the file path
stripPrefix: 'app/',
stripSufix: '.ext',
// prepend this to the
prependPrefix: 'served/',
// setting this option will create only a single module that contains templates
// from all the files, so you can load them all with module('foo')
moduleName: 'templates'
}
});
};
很抱歉复制粘贴了很多东西,但这是一个复杂的过程,我想确保我没有遗漏一小部分。那么,我在这里做错了什么导致了这个错误呢?问题是,当您在spec文件测试设置中获得模块时,您实际上是通过指定第二个参数来创建它,所以它只是清理在该模块下注册的所有内容 更改:
beforeEach(module('myApp.views.view1',[]));
到
而且,当您使用$controller
实例化控制器时,您需要提供作用域,因为您正在控制器中注入$scope
,并且不存在$scopeProvider
,它是一个特殊的注入依赖项,因此您需要提供它
大概是这样的:
it('should exist....', inject(function($controller, $rootScope) {
//spec body
var scope = $rootScope.$new();
var view1Ctrl = $controller('View1Ctrl', {$scope:scope});
expect(view1Ctrl).toBeDefined();
}));
抢手货但是现在我面临一个新的错误(当然)“错误:[$injector:unpr]未知提供程序:$scopeprovider您需要在本地提供范围。我会更新我的回答完美,谢谢您的帮助。我为此挣扎了一段时间