Getting Unknown Provider error when injecting a Service into an Angular unit test

a 夏天 提交于 2019-12-03 04:19:39

I just ran into this and solved it by switching to getting the service using the $injector explicitly:

var EventingService, $rootScope;
beforeEach(inject(function($injector) {
  EventingService = $injector.get('EventingService');
  $rootScope = $injector.get('$rootScope');
}));

I wish I could tell you why this works and why the simple

beforeEach(inject(function(EventingService) {   ....  }));

does not, but I don't have the time to investigate the internals. Always best to use one coding style and stick to it.

This style is better in that the name of the variable that you use in your tests is the correct name of the Service. But it is a bit verbose.

There is another angular magic feature that uses strange variable names like $rootScope but I don't like the hacky look of that.

Note that the most of the time people get this error because they didn't include the modules:

beforeEach(module('capsuling'));
beforeEach(module('capsuling.capsules.services'));

If your controllers (defined under dashboard.controllers module) depend on some services which are enclosed in different module (dashboard.services) than you need to reference the dependency modules in your module signature:

angular.module('dashboard.services', []);
angular.module('dashboard.controllers', ['dashboard.services']);

While this question is fairly old i lost significant time solving a problem similar to this one, i.e.:

Error: Unknown provider: SomeServiceProvider <- SomeService

Hence, i'm leaving here another possible cause to this issue. Hopefully, it would helpful to someone.

In my case, i had in my project two modules with the exactly same name but with different dependencies being created, i.e., two different .js files with:

angular.module('moduleName', [dependencies]))

From angular documentation:

Passing one argument retrieves an existing angular.Module, whereas passing more than one argument creates a new angular.Module

Conclusion: It turns out that what was being injected in the test was the module with the wrong dependencies. Removing the second argument from the module that was erroneously being created solved the problem.

user2562481

Have you tried defining an additional module that depends on your other aggregated modules like so:

angular.module( 'dashboard', [ 'dashboard.services', 'dashboard.controllers' ] )

So you can in the beforeEach specify the one module that has both submodules defined in it like so:

describe('Browse Controller Tests.', function () {
    beforeEach(function () {
        module('dashboard');
    });

    var controller, scope, eventingService;

    beforeEach(inject(function ($controller, $rootScope, EventingService) {
        scope = $rootScope.$new();
        eventingService = EventingService

        controller = $controller('Browse', {
            $scope: scope,
            eventing: eventingService
        });
    }));


    it('Expect True to be True', function () {
        expect(true).toBe(true);
    });
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!