Angularjs,Karma控制器单元测试注入器:modulerr
Angularjs & Karma controller unit test injector:modulerr
为了应用一些单元测试到我的AngularJS控制器,我无法使它正常工作…
这是我的Angularjs应用声明:
var ogcApp = angular.module('OGC', [
'ngAnimate',
'xeditable',
'ngRoute',
'ogc.services',
'ogc.controllers',
'restangular',
'AngularStomp',
'ui.bootstrap',
'ogc.directives',
'mgcrea.ngStrap',
'angularFileUpload',
'jlareau.pnotify',
'ngTagsInput'
]);
这是我的控制器原型:
angular.module('ogc.controllers').controller('applicantController', function ($scope, $log, $location, $cacheFactory, ngstomp, identityService, Restangular) { ...Some code... }
这是我的karma.conf.js:
module.exports = function(config) {
config.set({
// base path that will be used to resolve all patterns (eg. files, exclude)
basePath: '',
// frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
frameworks: ['jasmine'],
// list of files / patterns to load in the browser
files: [
'src/main/webapp/static/js/lib/angular/angular.min.js',
'src/main/webapp/static/js/lib/angular-animate/angular-animate.min.js',
'src/main/webapp/static/js/lib/angular-xeditable/dist/js/xeditable.min.js',
'src/main/webapp/static/js/lib/angular-route/angular-route.min.js',
'src/main/webapp/static/js/lib/restangular/dist/restangular.min.js',
'src/main/webapp/static/js/lib/AngularStompDK/dist/angular-stomp.min.js',
'src/main/webapp/static/js/lib/angular-bootstrap/ui-bootstrap-tpls.min.js',
'src/main/webapp/static/js/lib/angular-strap/dist/angular-strap.min.js',
'src/main/webapp/static/js/lib/ng-file-upload/angular-file-upload.min.js',
'src/main/webapp/static/js/lib/jquery/dist/jquery.min.js',
'src/main/webapp/static/js/lib/pnotify/pnotify.core.js',
'src/main/webapp/static/js/lib/ng-tags-input/ng-tags-input.min.js',
'src/main/webapp/static/js/lib/angular-mocks/angular-mocks.js',
'src/main/webapp/static/js/*.js',
'src/test/karma/**/*.spec.js'
],
// list of files to exclude
exclude: [
],
// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: {
},
// test results reporter to use
// possible values: 'dots', 'progress'
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: ['progress'],
// web server port
port: 9876,
// enable / disable colors in the output (reporters and logs)
colors: true,
// level of logging
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
logLevel: config.LOG_DEBUG,
// enable / disable watching file and executing tests whenever any file changes
autoWatch: true,
// start these browsers
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
browsers: ['PhantomJS'],
// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: false
});
};
这是我的简单测试:
describe('UNIT : applicantController', function() {
beforeEach(module('OGC'));
var ctrl, scope, aSearchForm;
// inject the $controller and $rootScope services
// in the beforeEach block
beforeEach(inject(function($controller, $rootScope) {
// Create a new scope that's a child of the $rootScope
scope = $rootScope.$new();
// Create the controller
ctrl = $controller('ogc.controllers.applicantController', {
$scope: scope
});
}));
it('should instantiate the controller properly', function () {
expect(ctrl).not.toBeUndefined();
});
})
我在karma服务器日志上得到了这个错误:
PhantomJS 1.9.7 UNIT : applicantController should instantiate the controller properly FAILED
Error: [$injector:modulerr] Failed to instantiate module restangular due to:
ReferenceError: Can't find variable: _
和一个简单的:
expect(true).toBe(true);
不工作,而beforeEach与注入在那里,所以我认为问题是在依赖注入,但在哪里?…
修改代码:
ctrl = $controller('ogc.controllers.applicantController', {
$scope: scope
});
:
ctrl = $controller('applicantController', {
$scope: scope
});
在angularjs中还没有真正的命名空间概念。所有来自不同模块的东西将被混合到同一个桶中。
p。作为一般提示,在调试时尝试使用未合并的angular.js
而不是angular.min.js
,它会给您一个更有意义的错误。
EDIT:原来还有一个问题:
lodash
或underscore
库缺失,Restangular
需要
相关文章:
- AngularJS 服务注入器错误
- 在运行时从Angular注入器检索实例
- 最小化httpInterceptor AngularJS$注入器错误
- AngularJS:骨架应用程序上的注入器模块rr错误
- 通过注入器获取url参数查询
- 如何修复角度缩小生成后的注入器错误
- 如何使用TypeScript静态注入器模式注入“$rootScope”
- 注入器使用什么逻辑来决定传递给控制器的作用域?
- 在指令中手动从注入器获取依赖
- Angularjs,Karma控制器单元测试注入器:modulerr
- AngularJs注入器模块错误
- 为什么我不能把服务注入到Angular的工厂/注入器中?
- 角注入器错误帮助>
- 注入器已创建.无法注册模块
- Angular注入器自调用函数语法错误
- AngularjS美元注入器:unpr
- 尝试使用角网格化时,Angular注入器出错
- Angular无法在IE9上工作,可能是由于内部注入器
- 角.注入器不像想象的那样工作
- Angular 2和ngmodule注入器