ABucin
ABucin

Reputation: 956

Cannot inject service into its Angular unit test

I'm trying to unit test an Angular service using:

The problem is, the service I wish to test (MyService) is not injected in the test file by the angular-mocks lib (i.e. the 'inject' method does nothing). My code looks as follows:

main.js

(function() {
    'use strict';

    angular.module('module', [ 'ngCookies', 'ngSanitize' ]);
})();

service.js

(function () {
    'use strict';

    angular.module('module')
        .factory('MyService', MyService);

    MyService.$inject = ['Dependency'];

    function MyService(Dependency) {
        return {
            method: method
        };

        function method() {
            // do something
        }
    }
})();

service.spec.js

(function () {
    'use strict';

    describe('MyService', function () {
        var deferred;
        var MyService;
        var DependencyMock = {};

        beforeEach(module('module'));

        beforeEach(module(function ($provide) {
            $provide.value('Dependency', DependencyMock);
        }));

        beforeEach(inject(function (_MyService_, $q) {
            MyService = _MyService_; // nothing is injected here
            deferred = $q.defer(); // nothing is injected here
        }));

        it('should be injected', function () {
            console.log(deferred); // logs 'undefined'
            expect(MyService).toBeDefined(); // fails
        });

        describe('method', function () {
            it('should have this method', function () {
                expect(MyService.method).toBeDefined();  // fails as MyService is undefined
                expect(typeof MyService.method).toBe('function');
            });
        });
    });
})();

karma.conf.js

    // list of files / patterns to load in the browser
    files: [
      'libs/angular/angular.js',
      'libs/angular-mocks/angular-mocks.js',

      'src/js/main.js',
      'src/js/services/service.js',

      'src/js/tests/unit/service.spec.js'
    ]

package.json

 "devDependencies": {
    "bower": "1.7.7",
    "grunt": "0.4.5",
    "grunt-contrib-clean": "0.7.0",
    "grunt-contrib-concat": "0.5.1",
    "grunt-contrib-connect": "0.11.2",
    "grunt-contrib-copy": "0.8.2",
    "grunt-contrib-cssmin": "0.14.0",
    "grunt-contrib-jshint": "0.12.0",
    "grunt-contrib-less": "1.1.0",
    "grunt-contrib-uglify": "0.11.0",
    "grunt-karma": "0.12.1",
    "grunt-lesshint": "1.1.1",
    "grunt-ngdocs": "0.2.9",
    "grunt-processhtml": "0.3.9",
    "jasmine-core": "2.4.1",
    "karma": "0.13.19",
    "karma-cli": "0.1.2",
    "karma-coverage": "0.5.3",
    "karma-jasmine": "0.3.6",
    "karma-phantomjs-launcher": "1.0.0",
    "karma-spec-reporter": "0.0.24",
    "phantomjs-prebuilt": "2.1.3"
  }

bower.json

"devDependencies": {
    "angular": "1.3.8",
    "angular-animate": "1.3.8",
    "angular-cookies": "1.3.8",
    "angular-mocks": "1.3.8",
    "angular-soap": "2.1.1",
    "bootstrap": "3.3.6",
    "font-awesome": "4.5.0",
    "angular-translate": "2.9.0",
    "angular-sanitize": "1.3.8"
}

My guess is that this is somehow related to angular-mocks.

Upvotes: 2

Views: 2696

Answers (2)

ABucin
ABucin

Reputation: 956

I've figured out the problem. It seems that one of the Angular module dependencies was not included by Karma, which caused the 'inject' to not work.

Upvotes: 2

Dan Osborne
Dan Osborne

Reputation: 189

Try injecting an instance of your service inside each it() method.

eg:

it('should be injected', inject(function ('MyService') {
        console.log(deferred);
        expect(MyService).toBeDefined();
    }));

    describe('method', function () {
        it('should have this method', inject(function ('MyService') {
            expect(MyService.method).toBeDefined(); 
            expect(typeof MyService.method).toBe('function');
        }));
    });

Upvotes: 0

Related Questions