javascript - Why does $httpbackend.flush cause my $scope.$watch to fire indefinitely? -


when running jasmine unit test angular controller, fails message

'error: 10 $digest() iterations reached. aborting!'   

when $httpbackend.flush() called.

this controller:

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

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

});

when set third parameter in $scope.$watch true, (compare object equality instead of reference), test passes.

why $httpbackend.flush() cause $watch trigger? , why watch trigger after that?

// **when assigning currentthing, trigger watch** $scope.currentthing = globalstate.getcurrentthing(); $scope.success = false;  $scope.$watch(globalstate.getcurrentthing, function(newvalue, oldvalue){     // **here changing item whom watching can cause recursion.**     $scope.currentthing = newvalue; }); 

Comments

Popular posts from this blog

Unable to remove the www from url on https using .htaccess -