我是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',
});

最佳答案

假设您要调用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
        }
      };

09-30 19:27
查看更多