2016-09-24 50 views
0

我有一个控制器内该功能:茉莉花类型错误:未定义是不是构造

$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(); 
    }); 
}); 

然后我得到了错误

“类型错误:未定义不是构造函数(近 '......}),然后(函数(RES))({...')”

。 这里有什么错误,我是单元测试新手? p.s.代码运行良好。

+0

@jlogan请问,你能帮忙吗? –

回答

0

在描述函数结束时出现错字 - 您未关闭父级(描述函数) - 请注意,我添加了注释仅用于描述目的。

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 
+0

谢谢,但在代码中这里有什么错? –

+0

@Ebrahim Ze - 最后的右括号缺失......这是上面代码段的最后一行..}); //关闭父级描述功能 – gavgrif

+0

那些在我的片段中丢失,但错误出现在控制器代码中,而不是在测试中。 –

相关问题