Reputation: 3733
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
Upvotes: 2
Views: 1468
Reputation: 3733
Ok turns out I was only flushing one of the backend calls. I added another httpBackend.flush() and both backend calls were flushed.
Upvotes: 0
Reputation: 1230
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
Upvotes: 0