Unknown provider in Karma test

I have the following provider:

(function (angular) { angular.module('app') .provider('$_Config', ConfigProvider); function ConfigProvider() { .... //routes definition } ConfigProvider.prototype.$get = function () { return this; }; ConfigProvider.prototype.getRoutes = function() {...} //other prototype functions })(angular); 

In app.js, I use it as follows:

 app.config(function ($routeProvider, $_ConfigProvider) { var routes = $_ConfigProvider.getRoutes(); routes.forEach(function(route) { $routeProvider .when(route.route, { ..... }) } 

Everything works fine until it comes to testing. Here is my test:

 describe('Provider: $_ConfigProvider', function () { // load the providers module beforeEach(module('app')); // instantiate provider var $_ConfigProvider; beforeEach(inject(function (_$_Config_) { $_ConfigProvider = _$_Config_; })); it('Should verify getRoutes function', function () { var routes = $_ConfigProvider.getRoutes(); expect(Object.prototype.toString.call(routes) === '[object Array]').toBe(true); }); }); 

When starting the test, I get the following error:

  Error: [$injector:modulerr] Failed to instantiate module app due to: Error: [$injector:unpr] Unknown provider: $_ConfigProvider 

Note: $_ConfigProvider is entered correctly at run time.

+5
source share
1 answer

You probably do not include the file in which the provider is listed in your karma.conf.js dependency list. See this question:

Include dependencies in a Karma test file for an Angular app?

I would rename $ _Config to something else, '$' is usually reserved for angular-specific components.

+4
source

Source: https://habr.com/ru/post/1213894/


All Articles