Cameron Cameron - 5 months ago 15
AngularJS Question

ngMock not injecting factory

I have been stuck on this problem for the past 24 hrs and cannot see what is wrong with my code here. I am getting an injector error from Angular and cannot understand why. Here is my code:

(function (){
'use strict';
angular.module('Test', []).factory('stats', factory);

function factory() {
return {
dummy: 'Dummy Text'
};
}
})

describe('Test', function() {
var stats;
beforeEach(module('Test'));

beforeEach(inject(function(_stats_) {
stats = _stats_;
}));

it('Should be defined', function() {
expect(stats).toBeDefined();
})
})


I have checked my karma.conf.js file and all the files needed are being included in the test. I have another service that belongs to the same module and when I try to inject it instead of 'test' it works as expected.

Any help on this would be greatly appreciated.

Answer

You're defining a function, which defines a Test module and adds a service stats, but you never call that function.

The code should be:

(function (){
  ...
})();

Note the additional pair of parentheses used to call the function.