AngularJS:对具有许多外部依赖项的控制器进行单元测试,引发太多错误

时间:2018-12-26 08:33:07

标签: angularjs unit-testing karma-jasmine angular-mock bardjs

好的,这就是问题。我检查了教程,文档和更多文章,发现有很多问题的人在对具有很多依赖关系的控制器进行单元测试时。

我不是一个依赖的兔子洞(使用BardJS可以解决此问题)。在此过程的每个步骤中,都会引发错误。目前,我面临以下问题:

  1. TypeError:未定义不是对象(正在评估'$ scope.utils.handleError')

以下是一个大项目,我正在尝试测试其中一个控制器:

// 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')"
  }

能为我提供以下帮助吗?

  1. 如何为如此复杂的Controller创建单元测试?
    1. 是否存在任何自动模拟方法?假设我不想测试cacheGETtaskUserIds,但是我仍然必须模拟 User.getTaskUserIds,为什么?
    2. 我如何加载文件吗?
    3. 是否有任何现代/更新/更简单的方法来测试AngularJS 1.x应用程序?

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'}
    ],

0 个答案:

没有答案