Phil Hudson Phil Hudson - 1 year ago 104
AngularJS Question

Angular 1.x service injection in unit tests

I've been trying different variations based on the info that's out there, but haven't got to a working solution yet.

I'm trying to inject a service into a unit test for testing.

My current implementation is below. The issue is, the service isn't:

ReferenceError: Can't find variable: SerializationService
- I've tried using
in the beforeeach too to no avail.


'use strict';

describe('SerializationService', function () {

var SerializationService;

beforeEach(function () {


inject(function (_SerializationService_) {
SerializationService = _SerializationService_;

it('should have a SerializationService', function () {

Other things I've tried:

  • Using separate module includes between the app module and injecting services in another foreach using the underscore notation


// Karma configuration
// Generated on 2016-08-15 using
// generator-karma 0.8.3

module.exports = function (config) {
'use strict';

// enable / disable watching file and executing tests whenever any file changes
autoWatch: true,

// base path, that will be used to resolve files and exclude
basePath: '../../',

// testing framework to use (jasmine/mocha/qunit/...)
frameworks: ['jasmine'],

// list of files / patterns to load in the browser
files: [

// list of files / patterns to exclude
exclude: [],

// web server port
port: 8081,

// Start these browsers, currently available:
// - Chrome
// - ChromeCanary
// - Firefox
// - Opera
// - Safari (only Mac)
// - PhantomJS
// - IE (only Windows)
browsers: [

// Which plugins to enable
plugins: [

// Continuous Integration mode
// if true, it capture browsers, run tests and exit
singleRun: false,

colors: true,

// level of logging
// possible values: LOG_DISABLE || LOG_ERROR || LOG_WARN || LOG_INFO || LOG_DEBUG
logLevel: config.LOG_INFO

//// Uncomment the following lines if you are using grunt's server to run the tests
// proxies: {
// '/': 'http://localhost:9000/'
// },
// //URL root prevent conflicts with the site root
// urlRoot: '_karma_'

Answer Source

Issue was in my mocking modules

I was using the crate method instead of the get:

This is wrong:

  .module('meow-mock', [])
  .factory('somemock', function () {

This is right:

  .factory('somemock', function () {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download