Error: [$injector:unpr] Unknown provider: in AngularJS Service Test

eb80 picture eb80 · Jan 6, 2014 · Viewed 46.1k times · Source

I am having a lot of trouble getting dependencies provided properly for an AngularJS service.

I see a number of other posts with similar errors here on StackOverflow but none of them seem to resolve the issue.

Here is the app code:

cm.modules.app = angular.module('myApp', ['ngRoute', 'ngAnimate']);
myServiceName = function($http) {
    // do stuff
};
myServiceName.prototype.value = 1;

cm.modules.app.service('defaultAlertFactoryA', myServiceName);

Here is the test code:

describe('test alertFactoryA', function() {
  var $provide;
  var mAlertFactoryA;

  beforeEach(module(cm.modules.app));

  beforeEach(angular.mock.module(function(_$provide_) {
    $provide = _$provide_;
  }));

  beforeEach(function() {
    inject(function($injector) {
      mAlertFactoryA = $injector.get('defaultAlertFactoryA');
    });
  });

  it('should work', function() {
    expect(true).toBe(true);
  });
});

Here is the error:

Error: [$injector:unpr] Unknown provider: defaultAlertFactoryAProvider <- defaultAlertFactoryA http://errors.angularjs.org/1.2.0-rc.2/$injector/unpr?p0=defaultAlertFactoryAProvider%20%3C-%20defaultAlertFactoryA

Question: How do I fix this so the test passes?

Answer

Eitan Peer picture Eitan Peer · Jan 7, 2014

In order to bootstrap your module you need to provide its name

beforeEach(module('myApp'));

Demo