使用$ interval.flush抛出指令的单元测试'可能是未处理的拒绝'

时间:2016-12-20 19:17:12

标签: angularjs unit-testing

我创建了使用$ interval service的指令:

directive('dir', ['$animate', '$interval', function($animate, $interval) {
    return {
      restrict: 'E',
      scope: {
        model:'='
      },
      controller: function($scope) {
        $scope.elements = [];

        this.register = function(element) {
          $scope.elements.push(element);
        }

        $scope.animate = function() {
          //do some animation
        }
      },
      link: function(scope) {
        scope.animation = $interval(function() {
          scope.animate();
        }, 500);

        scope.$watch('model', function(value) {
          if(value == false) {
            if(scope.animation) {
              $interval.cancel(scope.animation);
              scope.animation = undefined;
            }
          }
        });
      }
    };

当我为animate方法编写单元测试时,我会收到: 可能是未处理的拒绝:{}抛出

 beforeEach(inject(['$compile', '$rootScope', '$interval',
  function($compile, $rootScope, $interval) {
    var element = angular.element('<dir model="true"></dir>');
    var directive = $compile(element)($rootScope.$new());
    controller = directive.controller('loading');
    scope = directive.isolateScope();   
    interval = $interval;

    scope.$digest();
  }]));

  fit('trigger animation', function() {
    interval.flush(500);
  });

我可以理解这是因为我的链接函数中的$ interval返回的未解析的Promise。但无法弄清楚如何正确地做到这一点。

"angular": "~1.6.0",
"angular-mocks": "~1.6.0",

0 个答案:

没有答案