K
K
Konstantin Kitmanov2016-04-13 15:06:38
Angular
Konstantin Kitmanov, 2016-04-13 15:06:38

How to mock services (including built-in ones) for other services in Angular?

There is some simple module with a simple service:

// my.mdl.js
import angular from 'angular';
import myService from './services/my';

const myMdl= angular.module('module', []);
myMdl.factory(myService.name, myService);

export default myMdl;

// services/my.js
export default function myService ($q, anotherModuleService) {
    return {
        someFunc () {}
    }
}

// my.spec.js
import mocks from 'angular-mocks';
import myMdl from './my.mdl';

describe('myModule', () => {
    beforeEach(mocks.module(myMdl.name));

    describe('myModuleService', () => {
        let service;

        beforeEach(mocks.inject((myService) => { // LINE A
            service = myService;
        }));

        it('has expected method', () => {
            expect(service.someFunc).toEqual(jasmine.any(Function));
        });
    });
});

It works, but I need to lock $qand anotherModuleService. This is probably done somewhere in the LINE A area.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question