Patrick Reck
Patrick Reck

Reputation: 11374

Angular httpBackend: No pending request to flush

I am trying to use the $httpBackend for testing my AngularJS application. However, it keeps telling me that I have no pending requests.

This is the test:

it('should attempt to get boards from server', function() {
    httpBackend.when('GET', rootScope.serverRoot + '/boards').respond({id: 1});

    var boards;
    BoardServiceInstance.getBoards().then(function(data) {
        boards = data;
    });

    httpBackend.flush();

})

And the service:

this.getBoards = function() {
    return $http.get($rootScope.serverRoot + '/boards');
}

What am I doing wrong?

Upvotes: 0

Views: 331

Answers (1)

Patrick Reck
Patrick Reck

Reputation: 11374

Adding $rootScope.$digest(); did the trick.

Here's the final code:

it('should attempt to get boards from server', function() {
    $httpBackend.when('GET', '/boards').respond({id: 1});

    var boards;
    mockBoardService.getBoards().then(function(data) {
        boards = data;
    });

    $rootScope.$digest();

    $httpBackend.flush();

})

Upvotes: 1

Related Questions