Question

I'm getting the well-documented error below trying to run an Angular.js app through Karma:

[$injector:unpr] Unknown provider: $routeProvider

The JS is actually generated from ClojureScrpit using purnam. But looking at the generated JS, it looks ok:

var news = {app:{}};
news.app.newsApp = angular.module("newsApp", ["ngRoute"]);
angular.module("newsApp").config(["$routeProvider", function(a) {
  a.when("/", {templateUrl:"views/main.html", controller:"MainCtrl"});
  a.otherwise({redirectTo:"/"});
  return a;
}]);

So I am passing in 'ngRoute', which looks to be the common cause. My karma.conf.js includes angular-route:

files: [
  'app/bower_components/angular/angular.js',
  'app/bower_components/angular-mocks/angular-mocks.js',
  'app/bower_components/angular-resource/angular-resource.js',
  'app/bower_components/angular-cookies/angular-cookies.js',
  'app/bower_components/angular-sanitize/angular-sanitize.js',
  'app/bower_components/angular-route/angular-route.js',
  'app/scripts/*.js',
  'app/scripts/**/*.js',
  'test/mock/**/*.js',
  'test/spec/**/*.js'
],

And I see the angular-route loading in karma startup. Any help would be greatly appreciated.

Était-ce utile?

La solution

This seems to work at:

http://plnkr.co/edit/9xEZm0z22ab2YL6kL1fT?p=preview

var news = {app:{}};

news.app.newsApp = angular.module("newsApp", ["ngRoute"]);

news.app.newsApp.config(["$routeProvider", function(a) {
  a.when("/", {templateUrl:"views/main.html", controller:"MainCtrl"})
  a.otherwise({redirectTo:"/"});
  return a;
}]);

Autres conseils

Here is another try!

angular.module('newsApp').config(['$configName', '$routeProvider', function newsAppConfig(config, $routeProvider) {}]);

Hope this helps

I had the same issue and this was my solution. As you can see in the file karma.conf.js the javascript array named 'files[]' includes the folder /test/mock/**/*js. You can add in the folder 'mock' a file called modules.js. In this file add the following line;

'use strict';
 angular.mock.module('ngRoute', []);

So the line angular.mock.module('ngRoute', []); should do the trick. add more modules to mock in this file if necessary.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top