$ httpBackend.flush () throws method Error: [$ rootScope: infdig] 10 $ digest () iterations reached. Aborting

I am trying to run a unit test on my AngularJS application using Karma and Jasmine. I want to mock the $ http service. For this, I use the $ httpBackend method. Below is my service that I want to check:

angular.module('MyModule').factory('MyService', function($http, $log, $parse, $q, $timeout, $filter, MyOtherService1, MyOtherService2){ var service = {}; service.getSomething = function(id){ return $http.get('/somePath/subpath/' + id); } }); 

My unit test for this service:

 describe("myTest", function(){ var myService, $httpBackend, scope, mockMyOtherService1, mockMyOtherService2; var myResponse = { foo:'bar' }; beforeEach(module("MyModule")); beforeEach(inject(function(_MyService_, $injector){ $httpBackend = $injector.get('$httpBackend'); myService = _MyService_; scope = $injector.get('$rootScope').$new(); mockMyOtherService1 = $injector.get('MyOtherService1'); mockMyOtherService2 = $injector.get('MyOtherService2'); })); beforeEach(function(){ //To bypass dependent requests $httpBackend.whenGET(/\.html$/).respond(200,''); }); //If I uncomment the below afterEach block, the same error is shown at next line. /*afterEach(function() { $httpBackend.verifyNoOutstandingExpectation(); $httpBackend.verifyNoOutstandingRequest(); });*/ //This test passes successfully it("should check if service is instantiated", function () { expect(myService).toBeDefined(); }); //This test passes successfully it("should expect dependencies to be instantiated", function(){ expect($httpBackend).toBeDefined(); }); //The problem is in this test it("should get the getSomething with the provided ID", function() { $httpBackend.whenGET('/somePath/subpath/my_123').respond(200,myResponse); var deferredResponse = myService.getSomething('my_123'); //The error is shown in next line. $httpBackend.flush(); //If I comment the $httpBackend.flush(), in the next line, the $$state in deferredResponse shows that the Object that I responded with is not set ie it does not matches the 'myResponse'. expect(deferredResponse).toEqual(myResponse); }); }); 

This is an extreme problem and I need help regarding the same as soon as possible. I will be very grateful for your reply.

+5
source share
3 answers

The problem was that I needed to inject $ location into my spec files, even if they were not injected into the services. After the injection, everything worked well! Hope this helps someone who is stuck in the same situation.

+1
source

You will receive a promise from your service. So change your test code to:

 //The problem is in this test it("should get the getSomething with the provided ID", function (done) { $httpBackend.expectGET('/somePath/subpath/my_123').respond(200,myResponse); var deferredResponse = myService.getSomething('my_123'); deferredResponse.then(function (value) { expect(value.data).toEqual(myResponse); }).finally(done); $httpBackend.flush(); }); 
+1
source

I recently had this problem when upgrading a project from Angular 1.2 to 1.4. The verification code looked something like this:

 it('should call /something', function(){ httpBackend.expectGET('/something').respond(200); scope.doSomething(); httpBackend.flush(); }); 

The error was in infdig for 10 iterations. This is called by calling the .flush() method. I realized that it looks like there were no pending promises in doSomething() .

As soon as I added the promise somewhere inside doSomething() or internal methods, the infdig problem disappeared.

I suspect this is a 100% spec, so don't let it influence your development. This is because httpBackend does some tricks to wait for promises, which may be due to digestion until a change occurs. Since there are no promises, there are no changes - an endless digest.

+1
source

Source: https://habr.com/ru/post/1236617/


All Articles