Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to verify that an http request is not made at all?

Tags:

angularjs

how to verify that none of http request method are invoked to do any request. I have this code :

 $scope.getSubnetsPageDetails = function (pageNumber) {
        $http.get(URLS.subnetsPagesCount(pageNumber)).success(function (response) {
            $scope.pageDetails = response;
        }).error(function (response, errorCode) {

            });
    }; 

and this test :

it("should not allow request with negative page number", function () {

        scope.getSubnetsPageDetails(-1);
        //verify that htt.get is not invoked at all

    });

How to verify that http.get is not invoked ?

like image 961
Adelin Avatar asked Nov 13 '12 09:11

Adelin


1 Answers

You can test that no calls are made by using the verifyNoOutstandingRequest() method from the $httpBackend mock.

Usually those kind of verification is done in the afterEach section of a Jasmine's tests. On top of this it is common to call another method, verifyNoOutstandingExpectation() to verify that all the expected calls were actually invoked.

Here is the code, where you need to inject the $httpBackend mock:

var $httpBackend;
beforeEach(inject(function($injector) {
  $httpBackend = $injector.get('$httpBackend');
}));

then do you test and at the end:

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

Of course you could invoke the $httpBackend.verifyNoOutstandingRequest() inside an individual test. The mentioned http://docs.angularjs.org/api/ngMock.$httpBackend page has a wealth of information on the topic.

like image 85
pkozlowski.opensource Avatar answered Oct 13 '22 15:10

pkozlowski.opensource