Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 am using it like this:

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

}

Every thing work fine till it gets 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 running the test I am getting the following error:

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

Note: The $_ConfigProvider is injected correctly during run-time.

like image 976
vlio20 Avatar asked Sep 29 '22 16:09

vlio20


1 Answers

You are likely not including the file where the provider is defined in in your karma.conf.js dependencies list. See this question:

Include dependencies in Karma test file for Angular app?

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

like image 92
Peter Ashwell Avatar answered Oct 03 '22 11:10

Peter Ashwell