angularjshttpjasmineangular-promisehttpbackend

angular jasmine multiple nested httpbackend calls


I am trying to test a angularjs service with jasmine that calls a resource service and in the then function calls another nested resource function. I am using httpbackend to flush mock responses. The issue is that, while testing, it is not flushing the second response to the second nested resource service request and I am erred out with the error Error: Unflushed requests: 1

RestaurantService

var deferred;
var restaurantinfo = {};

var loadMenu = function(){
    return Menu.query({}).$promise.then(function(response){
        restaurantinfo.menu = response
    })
}

var loadLocation = function(){
    return Location.query({}).$promise.then(function(response){
        restaurantinfo.location = response;
    })
}

var getRestaurantInfo = function(){
    if(!deferred && (!restaurantinfo.menu.length == 0 || restaurantinfo.location.length === 0)){
        deferred = $q.defer();
            return loadMenu()
               .then(loadLocations)
               .then(function(){
                    deferred.resolve(billinghistory);
                    return deferred.promise;
                })
        } else {
            if(!deferred.promise.$$state.status){ return deferred.promise; }
            deferred.resolve(billinghistory);
            return deferred.promise;
        }
}

Test Spec

beforeEach(inject(function(_$httpBackend_, RestaurantService, _Menu_, _Locations_){
    httpBackend = _$httpBackend_;
    mockRestaurantService = _RestaurantService_;


    httpBackend.expect('GET', '/api/menu').respond(mockMenu);
    httpBackend.expect('GET', '/api/locations').respond(mockLocations);

    mockBalanceService.getRestaurantInfo();
    httpBackend.flush()

}))

afterEach(function(){
    httpBackend.verifyNoOutstandingExpectation();
    httpBackend.verifyNoOutstandingRequest();
})

the loadMenu and loadLocation functions just call angular Resource services

httpbackend is flushing the first call to api/menu but is not making the second call to api/location


Solution

  • Ok turns out I was only flushing one of the backend calls. I added another httpBackend.flush() and both backend calls were flushed.