Mats Mats - 1 month ago 13
Javascript Question

Yet another 'Unknown provider' for an AngularJS service

I actually hate to be that guy, but I've been sitting with this
problem for some days now. I have these three files as a part of a
larger angularjs application. I can not get even this rudimentary test
to pass (or even work). I've been comparing files within the project,
I've read on-line (tried all those ways people have suggested). I have
even written the files from scratch a few times. I'm probably not able
to see my error anymore. I guess this is easier to spot (right away)
for a back-seat driver.

I'd be most appreciative for any help.

The output from gulp/karma

PhantomJS 2.1.1 (Linux 0.0.0) SiteDescriptionService the service should be defined FAILED
Error: [$injector:unpr] Unknown provider: SiteDescriptionServiceProvider <- SiteDescriptionService
http://errors.angularjs.org/1.5.8/$injector/unpr?p0=SiteDescriptionServiceProvider%20%3C-%20SiteDescriptionService (line 4511)
bower_components/angular/angular.js:4511:86
getService@bower_components/angular/angular.js:4664:46
bower_components/angular/angular.js:4516:48
getService@bower_components/angular/angular.js:4664:46
injectionArgs@bower_components/angular/angular.js:4688:68
invoke@bower_components/angular/angular.js:4710:31
workFn@bower_components/angular-mocks/angular-mocks.js:3085:26
loaded@http://localhost:8080/context.js:151:17
inject@bower_components/angular-mocks/angular-mocks.js:3051:28
app/service/sitedescriptor-service-test.js:10:19
app/service/sitedescriptor-service-test.js:4:13
global code@app/service/sitedescriptor-service-test.js:1:9
Expected undefined to be truthy.
app/service/sitedescriptor-service-test.js:17:32
loaded@http://localhost:8080/context.js:151:17


The module declaration

(function(){
'use strict';

angular.module('application.service', []);

})();


The service itself

(function () {
angular.module('application.service')
.service('SiteDescriptorService',
['$http', '$q', function ($http, $q) {
var lastRequestFailed = true,
promise,
items = [];

return {
name: 'SiteDescriptorService',

getItems: function () {
if (!promise || lastRequestFailed) {
promise = $http.get('site.json').then(
function (response) {
lastRequestFailed = false;
items = response.data;
return items;
}, function (response) { // error
lastRequestFailed = true;
return $q.reject(response);
});
}
return promise;
}
};
}]
);

})();


and the test

describe('SiteDescriptionService', function() {
'use strict';

describe('the service', function() {

var service, httpBackend;

beforeEach(module('application.service'));

beforeEach(inject(function(_SiteDescriptionService_, $httpBackend) {
service = _SiteDescriptionService_;
httpBackend = $httpBackend;
console.log(service);
}));

it('should be defined', function() {
expect(service).toBeTruthy();
});

});
});


Cheers
Mats

Answer

Looks like you just use incorrect name when injecting dependency, should be 'SiteDescriptorService' and not 'SiteDescriptionService'