Reputation: 306
I have a service:
(function () {
angular.module('app').service('MyAppService', MyAppService);
MyAppService.$inject = ['$http', 'testUrl'];
function MyAppService($http, testUrl) {
var service = {
testFunction: testFunction
};
return service;
function testFunction() {
/*testurl is my backend API*/
return $http.get(testUrl)
.error(function(){
return;
})
.then(function (response) {
return response.data;
});
}
}
})();
I call this in my controller as:
testControllerFunction();
function testControllerFunction() {
MyAppService.testFunction().then(function (response) {
app.testResponse = response; //This my http response
console.log(app.testResponse);
});
}
I am writing a karma test case for successful $http.get request in the MyAppService as:
describe('MyAppService', function () {
var MyAppService,http;
beforeEach(function() {
module('app');
inject(function ($injector) {
MyAppService = $injector.get('MyAppService');
testUrl = $injector.get('testUrl');
http = $injector.get('$httpBackend');
});
});
it('should call the backend testurl ', function () {
MyAppService.testFunction();
http.expectGET(testUrl);
});
});
But this does not seem to be working? Where did I go wrong? Thanks!
Upvotes: 2
Views: 3236