2013-08-28 6 views
5

जब एक कोणीय नियंत्रक के लिए एक चमेली इकाई परीक्षण चल रहा है, यह संदेश

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

जब $ httpbackend.flush() कहा जाता है के साथ विफल रहता है।

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

यह मेरा 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); 
    }); 

});

मैं $ दायरे में तीसरे पैरामीटर सेट करते हैं। $, सच करने के लिए घड़ी (तुलना संदर्भ की बजाय समानता वस्तु), परीक्षण गुजरता है।

$ httpbackend.flush() $ घड़ी को गति प्रदान करने का कारण नहीं है क्यों? और घड़ी उसके बाद क्यों ट्रिगर करती है?

+0

'submitVenue' कहाँ परिभाषित किया गया है? – zsong

+0

इसे सबमिट करना था स्टफ()। उस समारोह को नियंत्रक में परिभाषित किया गया है। धन्यवाद। तदनुसार सवाल बदल दिया। – Torleif

+1

इस पर एक नज़र डालें, यह शायद मददगार हो सकता है। यह आपके परीक्षण से संबंधित नहीं है। http://stackoverflow.com/questions/13594732/maxing-out-on-digest-iterations?rq=1 – zsong

उत्तर

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; 
}); 
संबंधित मुद्दे