Jasmine TypeError: undefined不是构造函数

Jasmine TypeError : undefined is not a constructor

本文关键字:构造函数 undefined TypeError Jasmine      更新时间:2023-09-26

我在控制器中有这个函数:

  $scope.delt = function() {
                $scope.data = {};
                $scope.confirmPopup = $ionicPopup.confirm({
                    title: '<b>Delete user</b>',
                    template: "Are you sure you want to delete this user ?<br>can't be undo."
                }).then(function(res) {
                    if (res) {
                        API.editeTheUser.delete({ id: $scope.user.id }, function(res, header) {
                            $scope.addEvent('delete-user', 'Delete the user with phone_number :' + $scope.user.phone);
                            $rootScope.popup('delete', "delete was success");
                            $ionicHistory.goBack();
                        }, function(err) {
                          $rootScope.popup("Error", err.data.error);
                        });
                    } else {
                        console.log('You are not sure');
                    }
                });
            }

当我在单元测试中调用这个函数时:

    describe('manageUserCtrl', function() {
          var controller, window, scope,
              $rootScope,
              $q, store, API, $ionicPopup, deferredLogup;
    beforeEach(inject(function($controller, _$ionicPopup_, _$rootScope_, $q, _API_, _$window_) {
              $q = $q;;
              $ionicPopup = _$ionicPopup_;
              deferredLogup = $q.defer();
              $rootScope = _$rootScope_;
              spyOn($ionicPopup, 'confirm');
              scope = $rootScope.$new();
              API = _API_;
              window = _$window_; 
     controller = $controller('manageUserCtrl', {
                  '$scope': scope,
                  'API': API,
                  '$window': window,
                  '$ionicPopup': $ionicPopup
              });
          }));
    it('expect delete', function() {
          scope.delt();
    }); 
});

然后我得到了错误

"TypeError: undefined不是构造函数"…})(函数(res)){…")"

。这里的错误是什么,我是单元测试的新手?附:代码运行良好。

在描述函数的末尾有拼写错误——你没有关闭父级(描述函数)——注意,我添加注释只是为了描述的目的。

describe('manageUserCtrl', function() {
    var controller, window, scope,
        $rootScope,$q, store, API, $ionicPopup, deferredLogup;
    beforeEach(inject(function($controller, _$ionicPopup_, _$rootScope_, $q, _API_, _$window_) {
          $q = $q;;
          $ionicPopup = _$ionicPopup_;
          deferredLogup = $q.defer();
          $rootScope = _$rootScope_;
          spyOn($ionicPopup, 'confirm');
          scope = $rootScope.$new();
          API = _API_;
          window = _$window_; 
          controller = $controller('manageUserCtrl', {
              '$scope': scope,
              'API': API,
              '$window': window,
              '$ionicPopup': $ionicPopup
          }); // closes the controller
      })); //closes the beforeEachfuntion
    it('expect delete', function() {
          scope.delt();
    }); //closes the expect rule
}); //closes the parent describe function