【发布时间】:2014-09-28 05:23:50
【问题描述】:
为了对我的 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();
});
})
我在业力服务器日志上收到此错误:
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 存在时不起作用,所以我认为问题出在依赖注入上,但在哪里?..
【问题讨论】:
标签: javascript angularjs unit-testing controller karma-runner