2013-08-28 24 views
5

, denir mesajla

'Error: 10 $digest() iterations reached. Aborting!' 

$ httpbackend.flush() ile başarısız olur. Bu benim unittest olan

theApp.controller("myCtrl", function($scope, $http, globalstate){ 
    $scope.currentThing = globalstate.getCurrentThing(); 
     $scope.success = false; 

    $scope.$watch(globalstate.getCurrentThing, function(newValue, oldValue){ 
      $scope.currentThing = newValue; 
    }); 

    $scope.submitStuff = function(thing){ 
      $http.put('/api/thing/PutThing', thing, {params: {id: thing.Id}}) 
      .success(function(){   
       $scope.success = true; 
      }) 
    }; 
}); 

:

Bu

benim denetleyicisidir

describe('myCtrl', function(){ 

    var myController = null; 
    beforeEach(angular.mock.module('theApp')); 

    beforeEach(inject(function($injector){ 
     $rootScope = $injector.get('$rootScope'); 
     scope = $rootScope.$new(); 

     $httpBackend = $injector.get('$httpBackend'); 

     $controllerService = $injector.get('$controller'); 
     mockGlobalState = { 
      getCurrentThing : function(){ 
       return {Id: 1, name: 'thing1'}; 
      } 
     }; 

     $controllerService('myCtrl', {$scope: scope, globalstate: mockGlobalState}); 
    })); 

    it('should set flag on success', function(){ 
     var theThing = {Id: 2, name: ""}; 
     $httpBackend.expectPUT('/api/thing/PutThing?id=2',JSON.stringify(theThing)).respond(200,''); 

     scope.submitStuff(theThing, 0); 

     $httpBackend.flush(); 

     expect(scope.basicupdateSucceeded).toBe(true); 
    }); 

});

Üçüncü parametreyi $ kapsam olarak ayarladığımda. $ True değerine, (referans yerine nesne eşitliğini karşılaştırın), sınama geçer.

$ httpbackend.flush() $ saatinin neden tetiklenmesine neden oluyor? Ve saat neden bundan sonra kendini tetikliyor?

+0

'submitVenue' nerede tanımlandı? – zsong

+0

Bu submitStuff() olması gerekiyordu. Bu fonksiyon kontrolörde tanımlanmıştır. Teşekkürler. Soruyu buna göre değiştirdi. – Torleif

+1

Buna bir bakın, bu belki yararlı olabilir. Testlerinizle ilgili değil. http://stackoverflow.com/questions/13594732/maxing-out-on-digest-iterations?rq=1 – zsong

cevap

0
// **when you are assigning something to currentThing, it will trigger watch** 
$scope.currentThing = globalstate.getCurrentThing(); 
$scope.success = false; 

$scope.$watch(globalstate.getCurrentThing, function(newValue, oldValue){ 
    // **here you are changing the item to whom you are watching so it can cause recursion.** 
    $scope.currentThing = newValue; 
}); 
İlgili konular