JohanLarsson
JohanLarsson

Reputation: 485

Angular testing [$injector:unpr] Unknown provider: STARTUP_CONFIG

I'm having trouble getting my tests to run due to dependencies not beeing injected correctly.

The error I'm getting is defined in the title. I've included the actual test code, the app.js & index.html file from my solution.

The problem lies with the deferred bootstrap which I'm not fimiliar with as it was included by one of my colleagues. If I remove the "app.config(function (STARTUP_CONFIG..." from the app.js file then the test runs fine

How can I correctly inject the STARTUP_CONFIG in my test?

test code

..
..
describe("test description...", function () {

var app;
var mockupDataFactory;

beforeEach(module('Konstrukt'));

beforeEach(inject(function (STARTUP_CONFIG,BUDGETS,APPLICATIONS) {
    //failed attempt to inject STARTUP_CONFIG
}));

beforeEach(function () {
    app = angular.module("Konstrukt");
});

beforeEach(function () {
    mockupDataFactory = konstruktMockupData.getInstance();
});

it('should be accessible in app module', function () {
    expect(app.pivotTableService).toNotBe(null); //this test runs fine
});

it('test decr...', inject(function ( pivotTableService) {
    ... //Fails here
..
..

app.js

  ..
    ..        
    angular.module('Konstrukt', ['ngGrid', 'ngSanitize', 'ngRoute','pasvaz.bindonce', 'ngAnimate', 'nvd3ChartDirectives', 'ui.select', 'ngProgress', 'ui.grid', 'ui.grid.edit','ui.grid.selection', 'ui.grid.cellNav', 'ui.grid.pinning', 'ui.grid.resizeColumns']);

          var app = angular.module('Konstrukt');
          app.config(function (STARTUP_CONFIG, BUDGETS, APPLICATIONS) {

        var STARTUP_CONFIG = STARTUP_CONFIG;
        var BUDGETS = BUDGETS;
        var APPLICATIONS = APPLICATIONS;

      });
      ..
      ..

index.html

    ..
    ..

    <script>
  setTimeout(function(){
  window.deferredBootstrapper.bootstrap({
    element: window.document.body,
    module: 'Konstrukt',
    resolve: {
      STARTUP_CONFIG: ['$http', function ($http) {
        return $http.get('/scripts/_JSON/activeBudgets.JSON');
      }],
      BUDGETS: ['$http', function ($http) {
        return $http.get('/scripts/_JSON/activeBudgets.JSON');
      }],
      APPLICATIONS: ['$http', function ($http) {
        return $http.get('/scripts/_JSON/applications.JSON');
      }]
    }
  })
  } , 1500);
</script>

Upvotes: 0

Views: 293

Answers (1)

tasseKATT
tasseKATT

Reputation: 38490

The deferredBootstrapper will not run in your unit tests, which means the constants it normally adds to your module won't be available.

You can add a global beforeEach that provides mocked versions of them:

beforeEach(function () {
  module(function ($provide) {
    $provide.constant('STARTUP_CONFIG', { something: 'something' });
    $provide.constant('BUDGETS', { something: 'something' });
    $provide.constant('APPLICATIONS', { something: 'something' });
  });
});

Upvotes: 1

Related Questions