Angular mock $httpBackend give No pending request to flush
Solution 1
The _$httpBackend_
has nothing to flush because you don't make any http request in your test.
You need to invoke some code that make an http request.
Then, once something somewhere made an http request in your test, you can call the flush
method so that a response is provided for the request that has been made.
Something like:
it('should fetch list of users', function(){
// Do something that will make an http request
MyService.getAllUser(function ...) // for example
// Then provide a response for the http request that
// has been made when getAllUser was called
$httpBackend.flush();
// Check the expected result.
expect(something).toBe('Bob');
});
Solution 2
Same issue happened to me and the problem was not that I was not making a request but because the request I was making was different to the expected one:
For example I have defined this expectation:
mockBackend.expectGET("http://myapi.com/001").respond("RESULT");
And I was requesting this other URL:
http://myapi.com/002
Very confusing error message, no really easily related with the underneath problem.
Solution 3
I had the same issue, because I neglected to define a response for each expected request. With no response, there becomes nothing to flush. Also the http promise would never resolve or fail.
Related videos on Youtube
![Tres](https://i.stack.imgur.com/MXcMn.jpg?s=256&g=1)
Tres
Nicolò Tresoldi was born in Monselice (North Italy) in 1992. Currently enrolled in Computer Science at Padua University and previously licentiate as computer expert. He had a couple of abroad work experience in Liverpool and San Francisco. His favourite language is Javascript and he practices freestyle roller and downhill. He travelled a lot and expects to see all the European capitals within his 25 years old. In the free time he like to write in third person.
Updated on July 05, 2021Comments
-
Tres almost 3 years
Following the official guide at angularJS $httpBackend I'll do this test, but Karma give me this error:
Error: No pending request to flush ! at Function.$httpBackend.flush
Test
'use strict'; describe('profileCtrl', function () { var scope, $httpBackend; beforeEach(angular.mock.module('maap')); beforeEach(angular.mock.inject(function($rootScope, $controller, _$httpBackend_){ $httpBackend = _$httpBackend_; $httpBackend.when('GET', 'profile').respond([{id: 1, name: 'Bob'}]); scope = $rootScope.$new(); $controller('profileCtrl', {$scope: scope}); })) it('should fetch list of users', function(){ $httpBackend.flush(); expectGET(scope.current_user.length).toBe(1); expect(scope.current_user[0].name).toBe('Bob'); });
});
for this simple controller:
'use strict'; angular.module('maap').controller('profileCtrl', function($scope, UserService) { $scope.current_user = UserService.details(0);
});
-
madhured over 10 yearsis it warning or error?
-
Tres over 10 years@madhured It is an error
-
-
Ruben over 7 yearssame issue here but I can't guess the url I'm requesting. How do you know it? There are a way to show on logs or something?
-
Jack Ryan over 6 yearsThanks for this, I was trying to figure out why my code wasn't making an http request when it obviously should have been. Turns out I just had a typo in the
expectPOST
url.