For testing purposes, I am running checks on my Service
using httpBackend
. Specifically, I am focusing on a simple get
request that needs to be tested for the response code. However, despite my efforts, I keep encountering this error:
TypeError: undefined is not a function (evaluating '$httpBackend.expectGET('/api/something').response({})') in test/spec/services/requests.js (line 22)
test/spec/services/requests.js:22:51
Error: Unsatisfied requests: GET /api/something in /bower_components/angular-mocks/angular-mocks.js (line 1812)
verifyNoOutstandingExpectation@/bower_components/angular-mocks/angular-mocks.js:1812:74
/test/spec/services/requests.js:28:48
This is the service setup:
angular.module('requestService', [])
.factory('Request', ['$http', 'localConfig', 'Query', function($http, localConfig){
return {
getRequest: function(fromDate, toDate) {
return $http({
url: '/api/something',
method: "GET",
params: {fromDate: fromDate,
toDate: toDate},
headers: {
'Content-Type': 'application/json; charset=utf-8'
}
})
}
}
}]);
This is how the testing process is structured:
describe('Service: Request', function () {
var reqService, $httpBackend;
beforeEach(module('webapp'));
beforeEach(module('requestService'));
beforeEach(inject(function(Request, _$httpBackend_){
reqService = Request;
$httpBackend = _$httpBackend_;
}));
it('should test the response to be 200', function(){
$httpBackend.expectGET('/api/something').response({});
$httpBackend.flush();
});
afterEach(function(){
$httpBackend.verifyNoOutstandingExpectation();
$httpBackend.verifyNoOutstandingRequest();
})
});
EDIT
Following recommendations, changes were made to the test:
it('should test the response to be 200', function(){
console.log('service: ' + reqService);
reqService.getRequest('2016-03-26', '2016-03-29');
$httpBackend.expectGET('/api/something').respond({});
$httpBackend.flush();
});
Unfortunately, a new issue has arisen:
Error: Unexpected request: GET /api/something?fromDate=2016-03-26&toDate=2016-03-29
Expected GET /api/something in /bower_components/angular-mocks/angular-mocks.js (line 1403)
$httpBackend@/bower_components/angular-mocks/angular-mocks.js:1403:90
sendReq@/bower_components/angular/angular.js:11235:21
serverRequest@/bower_components/angular/angular.js:10945:23
processQueue@/bower_components/angular/angular.js:15552:30
/bower_components/angular/angular.js:15568:39
$eval@/bower_components/angular/angular.js:16820:28
$digest@/bower_components/angular/angular.js:16636:36
flush@/bower_components/angular-mocks/angular-mocks.js:1778:45
/test/spec/services/requests.js:24:23
Error: [$rootScope:inprog] $digest already in progress
http://errors.angularjs.org/1.5.0/$rootScope/inprog?p0=%24digest in bower_components/angular/angular.js (line 17178)
beginPhase@/bower_components/angular/angular.js:17178:88
$digest@//bower_components/angular/angular.js:16616:19
verifyNoOutstandingExpectation@/bower_components/angular-mocks/angular-mocks.js:1810:45
/test/spec/services/requests.js:29:48