2013-08-28 11 views
5

Podczas uruchamiania testów jednostkowych Jasmine dla kątowym regulatora, to nie z komunikatem

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

gdy $ httpbackend.flush() jest wywoływana.

To jest mój kontroler:

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

To mój unittest:

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

});

Kiedy ustawiam trzeci parametr w $ scope. $ Watch na true, (porównaj obiekt równości zamiast odniesienia), test przechodzi.

Dlaczego funkcja $ httpbackend.flush() powoduje uruchomienie funkcji $ watch? A dlaczego zegarek sam się uruchamia po tym?

+0

Gdzie zdefiniowano "submitVenue"? – zsong

+0

To miało być submitStuff(). Ta funkcja jest zdefiniowana w kontrolerze. Dzięki. Odpowiednio zmieniono pytanie. – Torleif

+1

Spójrz na to, może być pomocne. Nie odnosi się to do twoich testów. http://stackoverflow.com/questions/13594732/maxing-out-on-digest-iterations?rq=1 – zsong

Odpowiedz

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; 
}); 
Powiązane problemy