nourdine
nourdine

Reputation: 7597

Unknown provider in angular unit testing

I have this controller:

angular.module("controllers").controller("AddController", function($rootScope, $scope, $location, $timeout, $routeParams, Todos, MessageQueue) {
   // ... code
});

and this test:

describe("AddController", function() {

   var ctrl;
   var scope;

   beforeEach(module('controllers'));

   beforeEach(inject(function($rootScope, $controller) {
      scope = $rootScope.$new();
      ctrl = $controller('AddController', {
         $scope: scope
      });
   }));

   it("should be available", function() {
      expect(ctrl).not.toBe(undefined);
   });

});

Karma says:

Error: [$injector:unpr] Unknown provider: $routeParamsProvider <- $routeParams
    http://errors.angularjs.org/1.2.27-build.533+sha.c8c2386/$injector/unpr?p0=%24routeParamsProvider%20%3C-%20%24routeParams

But in my karma.config I am importing bower_components/angular-route/angular-route.js. Here:

module.exports = function(config) {
   config.set({
      basePath: '',
      frameworks: ['jasmine'],
      files: [
         'bower_components/angular/angular.js',
         'bower_components/angular-route/angular-route.js',
         'bower_components/angular-mocks/angular-mocks.js',

         'app/scripts/app.js',
         'app/scripts/services/mod.js',
         'app/scripts/directives/mod.js',
         'app/scripts/controllers/mod.js',

         'app/scripts/**/*.js',
         'test/spec/**/*Spec.js'
      ],
      exclude: [
      ],
      preprocessors: {
      },
      reporters: ['progress'],
      port: 9876,
      colors: true,
      logLevel: config.LOG_INFO,
      autoWatch: true,
      browsers: ['Firefox'],
      singleRun: false
   });
};

I am a bit puzzled. Anyone?

Upvotes: 4

Views: 4889

Answers (1)

David Bohunek
David Bohunek

Reputation: 3201

You can pass the '$routeParams' object yourself to the $controller call, it would look like this then:

beforeEach(inject(function($rootScope, $controller) {
      scope = $rootScope.$new();
      ctrl = $controller('AddController', {
         $scope: scope,
         $routeParams: {id: '...'}
      });
   }));

Upvotes: 4

Related Questions