2016-09-24 51 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

那些在我的片段中丟失,但錯誤出現在控制器代碼中,而不是在測試中。 –

相關問題