-1

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.

icanbeacoder
  • 1,388
  • 1
  • 13
  • 30

1 Answers1

3

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

hassassin
  • 5,024
  • 1
  • 29
  • 38
  • This helped me. expectPOST() was throwing the error, but when i used when(), the error dismissed. and test cases passed -_- – icanbeacoder Feb 18 '14 at 06:12