好的,这就是问题。我检查了教程,文档和更多文章,发现有很多问题的人在对具有很多依赖关系的控制器进行单元测试时。
我不是一个依赖的兔子洞(使用BardJS可以解决此问题)。在此过程的每个步骤中,都会引发错误。目前,我面临以下问题:
以下是一个大项目,我正在尝试测试其中一个控制器:
// controller.js
var myTask = angular.module('myTask',['angularUtils.directives.dirPagination', 'ngCsv']);
myTask.controller('myTaskIndexController', ['$scope', '$routeParams', 'ProgramUser', 'User', 'TaskUser', 'Task', 'TaskResponse', 'ProfileInstance', function($scope, $routeParams, ProgramUser, User, TaskUser, Task, TaskResponse, ProfileInstance){
this.arrayToObject = function(array,key) {
var map = {}
for (var i = 0; i < array.length; i += 1) {
map[array[i][key]]=array[i];
}
return map;
};
//callback function defined below as a global var
cacheGETtaskUserIds(User, function(){
//..code
};
$scope.utils.handleError();
}//@end CONTROLLER
这是在'User'
var taskUserIds;
function cacheGETtaskUserIds(User,callback) {
if(taskUserIds && taskUserIds.length)
return callback();
User.getTaskUserIds( function(taskIdsObj) {
taskUserIds = taskIdsObj.taskUserIds;
return callback();
});
}//@:end
这是测试文件,我只想测试this.arrayToObject
函数
// controller.spec.js :
describe('myTask-TaskIndexController', function() {
beforeEach(angular.mock.module('myTask'));
var myController;
var $controller, $rootScope;
var routeParamsMock, ProgramUserMock, UserMock, TaskUserMock, TaskMock, TaskResponseMock, ProfileInstanceMock;
beforeEach(inject(function( _$controller_, _$rootScope_){
$controller = _$controller_;
$rootScope = _$rootScope_.$new();
routeParamsMock = {};
ProgramUserMock = {};
//UserMock for User.getTaskUserIds in the global cacheGETtaskUserIds
UserMock = { getTaskUserIds: function() {
return { 1: 'Beverages', 2: 'Condiments' };
}
};
TaskUserMock ={};
TaskMock = {};
TaskResponseMock = {};
ProfileInstanceMock = {};
myController = $controller('myTaskIndexController', { $scope: $rootScope.$new(), $routeParams: routeParamsMock, ProgramUser: ProgramUserMock, User: UserMock, TaskUser: TaskUserMock, Task: TaskMock, TaskResponse: TaskResponseMock, ProfileInstance: ProfileInstanceMock });
}));
此配置本身为我抛出了错误:
PhantomJS 2.1.1 (Mac OS X 0.0.0) ERROR
{
"message": "An error was thrown in afterAll\nTypeError: undefined is not an object (evaluating 'c.module')",
"str": "An error was thrown in afterAll\nTypeError: undefined is not an object (evaluating 'c.module')"
PhantomJS 2.1.1 (Mac OS X 0.0.0) ERROR
{
"message": "An error was thrown in afterAll\nTypeError: undefined is not an object (evaluating 'c.module')",
"str": "An error was thrown in afterAll\nTypeError: undefined is not an object (evaluating 'c.module')"
}
能为我提供以下帮助吗?
Karma.conf.js文件加载模式
files: [
// module dependencies and angular mocks library
'../node_modules/babel-polyfill/dist/polyfill.js', //-->For Promises in PhantomJS
'../src/lib/angular-1.4/angular-route.min.js',
'../node_modules/angular/angular.js',
'../node_modules/angular-mocks/angular-mocks.js',
'../node_modules/bardjs/bard.js',
'../src/lib/datatables/js/dirPagination.js',
'../src/lib/ng-csv/ng-csv.js',
'../src/app/task/Module.js',
'../src/app/task/Controller.js',
//Test files
{pattern: './app/**/*.js'}
],