I\'m struggling with testing an AngularJS factory using Karma + Jasmine.
I am unable to inject my factory to OfficerValidationService
variable.
To add to martynas' answer, here is a simplified version:
module()
is the shorthand of angular.mock.module
inject()
is the shorthand of angular.mock.inject (same as $injector)
'use strict';
(function() {
describe('OfficerValidationService Spec', function() {
var OfficerValidationService;
beforeEach(module('darthvader'));
beforeEach(inject(function (OfficerValidationService) {
OfficerValidationService = OfficerValidationService;
}));
it('OfficerValidationService should exist', function() {
expect(OfficerValidationService).toBeTruthy();
});
});
}());
You need to call angular.injector
:
'use strict';
(function() {
describe('OfficerValidationService Spec', function() {
var OfficerValidationService;
beforeEach(function() {
angular.module('darthvader');
});
beforeEach(inject(function() {
var $injector = angular.injector(['darthvader']);
OfficerValidationService = $injector.get('OfficerValidationService');
}));
it('is very true', function(){
var output = OfficerValidationService.something();
expect(output).toBeTruthy();
});
});
}());