Angularjs Angular.js,$timeout.flush导致指令中的无限摘要循环';s单元测试

Angularjs Angular.js,$timeout.flush导致指令中的无限摘要循环';s单元测试,angularjs,unit-testing,angularjs-directive,timeout,karma-runner,Angularjs,Unit Testing,Angularjs Directive,Timeout,Karma Runner,我正试图用jasmine和karma为我的angular应用程序编写单元测试 $timeout.flush在指令的单元测试中导致无限摘要循环 这是我的指令码 .directive("myDirective", function($timeout) { return { link: function(scope) { $timeout(function() { console.log("myDirective after

我正试图用jasmine和karma为我的angular应用程序编写单元测试

$timeout.flush在指令的单元测试中导致无限摘要循环

这是我的指令码

   .directive("myDirective", function($timeout) {
      return {
        link: function(scope) {
          $timeout(function() {
            console.log("myDirective after timeout");
            scope.testNum = 1;
          },1000);
        }
      };
    });
下面是单元测试代码

  describe("Directive: myDirective", function() {
    var scope, element, $timeout;
    beforeEach(function() {
      angular.mock.module("myApp");
      angular.mock.inject(function($compile, $rootScope, _$timeout_) {
        $timeout = _$timeout_;
        scope = $rootScope.$new();
        var directiveElement = angular.element(
          "<my-directive></my-directive>"
        );
        element = $compile(directiveElement)(scope);
        scope.$digest();

      });
    });

    it('should update scope.testNum', function() {
      $timeout.flush();
      expect(scope.testNum).toBe(1);
    });
  });
description(“指令:myDirective”,函数(){
变量范围,元素,$timeout;
beforeach(函数(){
角度模拟模块(“myApp”);
angular.mock.inject(函数($compile、$rootScope、$timeout){
$timeout=$timeout;
scope=$rootScope.$new();
var directiveElement=angular.element(
""
);
元素=$compile(directiveElement)(范围);
范围。$digest();
});
});
它('应该更新scope.testNum',函数(){
$timeout.flush();
expect(scope.testNum).toBe(1);
});
});
我犯了这个错误

Chrome 47.0.2526(Mac OS X 10.10.4)指令:myDirective应更新scope.testNum失败错误:[$rootScope:infdig] $rootScope/infdig?p0=10&p1=%5B%5D

错误(本机)

在web/public/javascripts/lib/angular.min.js:6:416

r.$digest(web/public/javascripts/lib/angular.min.js:131:499)

r.$apply(web/public/javascripts/lib/angular.min.js:134:78)

在Object.fn(web/public/javascripts/lib/angular.min.js:146:449)

在Function.self.defer.flush(web/public/javascripts/lib/angular mocks.js:126:32)

函数$delegate.flush(web/public/javascripts/lib/angular mocks.js:1750:20)

反对。(web/test/unit/mainSpecs/myDirectiveSpec.js:35:16)


错误中的测试名称是
应该在超时后记录
,而您发布的测试名称是
应该更新scope.testNum
。这些是相同的吗?是的,这些是相同的,我更新了错误文本。