【发布时间】:2015-02-06 16:39:15
【问题描述】:
我正在尝试使用 karma 将单元测试添加到角度控制器,但我遇到了一个问题,我需要一些帮助。我在这里发现了类似的问题,但没有一个让我得到答案。我尝试更改 karma.conf.js 中“文件”数组的顺序,认为它们可能没有正确加载,但没有产生任何结果。当我运行“karma start karma.conf.js”时出现错误
来自业力的控制台错误
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();
}));
});
});
这是它正在测试的视图控制器
view1.js
'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!";
}]);
目录结构
/
.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' 的指令 .html 模板
karma.conf.js
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'
}
});
};
对于复制粘贴大量内容深表歉意,但这是一个复杂的过程,我想确保我不会遗漏一小部分。那么我在这里做错了什么导致了这个错误?
【问题讨论】:
标签: javascript angularjs jasmine karma-runner karma-jasmine