Injecting dependent services when unit testing AngularJS services

后端 未结 5 1487
隐瞒了意图╮
隐瞒了意图╮ 2020-12-08 00:06

I\'m testing service A, but service A depends on service B (i.e. service B is injected into service A).

I\'ve seen this question but my case is a bit different becau

5条回答
  •  执念已碎
    2020-12-08 00:55

    The Valentyn solution worked for me, but there is another alternative.

    beforeEach(function () {
    
        angular.mock.module("moduleThatContainsServiceA", function ($provide) {
                    $provide.value('B', ...);
                });
    });
    

    Then when AngularJS service A request the Service B by Dependency Injection, your mock of Service B will be provided instead of the Service B from moduleThatContainsServiceA.

    This way you don't need to create an additional angular module just to mock a Service.

提交回复
热议问题