2

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

2
  • please can you put the angualr code of the promises in detail. Commented Nov 18, 2016 at 6:43
  • Updated as much as I can, the software I work on is pretty proprietary. Commented Nov 18, 2016 at 6:54

2 Answers 2

0

th property : 'returnValue' to mock the http service, and make a spyOn to the call

var error = {
                code : 500,
                mensaje1:'err'
            },

                spyOn(getRestaurantInfo)
                .and.returnValue({
                    then: function (callbackSuccess, callbackError) {
                        callbackError(error);
                    }
                }); 

This will be a promise for error. Tell me if well for your code. thanks

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.