2017-03-12 77 views
0

我是AngularJS和Jasmine的新手。鉴于以下控制器,如何在$ scope.getPanels被触发时测试是否调用allPanelsRetrieved()函数?在此先感谢当另一个函数被触发时检查函数是否被调用

angular. 
module('panelList'). 
    controller('PanelListController', ['Panel', 'PanelSelection', '$scope', '$location', '$uibModal', '$rootScope', 
    function PanelListController(PanelSelection, $scope, $location, $uibModal, $rootScope) { 

     $scope.maxAbv = 2; 
     $scope.minAbv = 12; 


     this.allPanelsRetrieved = (index, before, filterParams) => { 
     ..... 
     }; 

     $scope.getPanels =() => { 
     const filterParams = {}; 
     filterParams.abv_lt = $scope.minAbv; 
     filterParams.abv_gt = $scope.maxAbv; 


     $scope.currentPagePanels = this.allPanelsRetrieved (1,[], filterParams); 
     }; 

}]). 
component('panelList', { 
    templateUrl: '/components/panel-list/panel-list.template.html', 
    controller:'PanelListController', 
}); 
+0

将'console.log('called')'添加到它 – Ibu

回答

1

假设你想allPanelsRetrived被调用,然后只需使用一个布尔值。

var bool = false 

this.allPanelsRetrieved = (index, before, filterParams) => { 
     ..... 
     bool=true; 
     }; 

     $scope.getPanels =() => { 
     if (bool) { 
     const filterParams = {}; 
     filterParams.abv_lt = $scope.minAbv; 
     filterParams.abv_gt = $scope.maxAbv; 

     $scope.currentPagePanels = this.allPanelsRetrieved (1,[], filterParams); 

     } else { 
      // allPanelsRetrieved was not called 
     } 
     }; 
1

我可以看到allPanelsRetrieved似乎是一个私有的(本地)方法和控制器内部使用。

  1. 您不需要测试私有(本地)方法的执行。
  2. 如果你仍然想检查方法被触发与否可以用茉莉花的toHaveBeenCalled()方法

    execept(myMethod的).toHaveBeenCalled();调用方法时传递 。