检查在触发另一个函数时是否调用函数

时间:2017-03-12 20:46:22

标签: javascript angularjs jasmine

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

2 个答案:

答案 0 :(得分: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 :(得分:1)

我可以看到 allPanelsRetrieved 似乎是一个私有(本地)方法,并在该控制器中使用。

  1. 您无需测试私有(本地)方法执行。
  2. 如果您仍想检查方法是否被触发,可以使用jasmine的List<Shirt> shirts = new List<Shirt>(); 方法

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