Tres Tres - 1 year ago 194
AngularJS Question

Angular mock $httpBackend give No pending request to flush

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


'use strict';

describe('profileCtrl', function () {
var scope, $httpBackend;

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(){


for this simple controller:

'use strict';

angular.module('maap').controller('profileCtrl', function($scope, UserService) {

$scope.current_user = UserService.details(0);


Answer Source

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

    // Check the expected result.
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download