Search code examples
angularjsunit-testingjasminekarma-runnerhttpbackend

$httpBackend handling more than 3 calls


I am currently running angularjs 1.2.10 and using karma/jasmine with angular-mocks-1.2.10 for unit testing and stuck in unit test case for $httpBackend.

inside it block

describe('sumCtrl'...)
 ...
beforeEach(angular.mock.inject(function($rootScope,$controller,$httpBackend){
 scope = $rootScope.$new();
 httpBackend = $httpBackend;
 $controller('sumCtrl',{$scope:scope});
}));

it("should call these http services",function(){
 httpBackend.expectGET('/api/something1/').respond({success:true});
 httpBackend.expectPOST('/api/something2/').respond({success:true});
 httpBackend.flush();

});

The above code works perfectly but when I add one more httpBackend call

    it("should call these http services",function(){
     httpBackend.expectGET('/api/something1/').respond({success:true});
     httpBackend.expectPOST('/api/something2/').respond({success:true});
     httpBackend.expectPOST('/api/something3/').respond({success:true});
     httpBackend.flush();

    });

This gives error on line 4. Unsatisfied Request: POST '/api/something3' .... use $httpBackend....

Don't know whether there is a limit to number of requests made using $httpBackend in it block or something else that needs to be kept in mind while using $httpBackend.


Solution

  • I made a fiddle that should you can have more than 2: http://fiddle.jshell.net/gZS5M/

    $http.get('/api/something1');
    $http.post('/api/something2');
    $http.post('/api/something3');
    $http.post('/api/something4');
    

    Works fine. I'm guessing you just have a typo in your url or something.

    Feel free to shoot back at me if this doesn't help