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

how to verify that none of the http request methods are used to execute 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 check that http.get is not being called?

+6
source share
1 answer

You can verify that calls are not being made using the verifyNoOutstandingRequest() method from $ httpBackend mock .

Typically, this check is performed in the afterEach section of afterEach tests. In addition, to call another method, verifyNoOutstandingExpectation() , you must verify that all expected calls have actually been called.

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

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

then you test and at the end:

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

Of course, you can call $httpBackend.verifyNoOutstandingRequest() inside a separate test. The mentioned page http://docs.angularjs.org/api/ngMock.$httpBackend contains a lot of information on this topic.

+13
source

All Articles