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

时间:2015-12-03 12:27:19

标签: angularjs unit-testing angularjs-directive timeout karma-runner

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

$ 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);
    });
  });

我收到了这个错误

  

Chrome 47.0.2526(Mac OS X 10.10.4)指令:myDirective应更新scope.testNum FAILED错误:[$ rootScope:infdig]   http://errors.angularjs.org/1.4.8/ $ rootScope / infdig P0 = 10安培; P1 =%5B%5D

     

出现错误(原生)

     

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

     

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

     

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

     

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

     

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

     

在Function。$ delegate.flush(web / public / javascripts / lib / angular-mocks.js:1750:20)

     

at Object。 (网络/测试/单元/ mainSpecs / myDirectiveSpec.js:35:16)

0 个答案:

没有答案