mindparse
mindparse

Reputation: 7215

Mocking a service in another service from a different module

I have a service that depends on another service from a different module like so:

(function() {
  'use strict';

  angular
    .module('app.core')
    .factory('userService', userService);

  function authService() {

        return: {
            userLoggedIn: false
        }
    }

})();


(function() {
  'use strict';

  angular
    .module('app.services')
    .factory('AuthService', authService);

  authService.$inject = ['$http', 'userService'];

  function authService($http, userService) {
    }

I'm trying write tests for my authService but am getting injection errors since it can't find userService

beforeEach(function() {
    module('app.services');
});
beforeEach(inject(function(_AuthService_) {
    authService = _AuthService_;

}));

How can I overcome this, will using $provide help me here?

UPDATE

I have attempted the following, but still getting the error

beforeEach(function() {
    module('app.services');
});

beforeEach(inject(function(_AuthService_, _$provide_) {
    authService = _AuthService_;
    $provide = _$provide_;
}));

beforeEach(function() {
    module(function ($provide) {
        $provide.value('userService', function(){
            return {
                userLoggedIn: false
            }
        });
    });
});

SOLVED

Ok, so I just needed to do the following:

beforeEach(function() {
    module('app.dataservices');
    module(function ($provide) {
        $provide.value('userService', function(){
            return {
                userLoggedIn: false
            }
        });
    });
});

beforeEach(inject(function(_AuthService_) {
    authService = _AuthService_;
}));

Tests are now passing fine for me

Upvotes: 1

Views: 1036

Answers (2)

danday74
danday74

Reputation: 56936

you should be preloading all services in your karma.conf.js (i assume you are using karma).

here is our karma.conf.js file ...

/** * Karma test runner configuration */ 'use strict';

module.exports = function (config) {
 config.set({
basePath: './',
browsers: ['PhantomJS'],
frameworks: ['jasmine'],
reporters: ['mocha', 'coverage'],
singleRun: true,
preprocessors: {
  'src/**/!(*spec)*.js': ['coverage'],
  'dest/**/*.html': ['ng-html2js']
},
ngHtml2JsPreprocessor: {
  stripPrefix: 'dest/',
  moduleName: 'ngHtmlFiles'
},
coverageReporter: {
  type: 'html',
  dir: 'coverage'
},
files: [
  'dest/vendor.min.js',
  'bower_components/angular-mocks/angular-mocks.js',
  'src/**/*.js',
  'dest/**/*.html'
]
});
};

Upvotes: 0

Bolza
Bolza

Reputation: 1944

Let's say you service uses the $state service and you want to mock id. Specifically the get method. Then you just need to add inside the first describe something like this.

beforeEach(function () {
        module(function ($provide) {
            $provide.service('$state', function() {
                return {
                    get: function() {}
                }
            });
    });
});

In this gist you can find some interesting examples of mocking services using $provide.

Upvotes: 3

Related Questions