Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to mock providers/config in module config for unit test

I am trying to set up unit testing with Angular and have hit a bit of a wall with injecting into the module level config and run methods.

For example, if I have a module definition as such:

angular.module('foo', ['ngRoute', 'angular-loading-bar', 'ui.bootstrap']).config(function ($routeProvider, $locationProvider, datepickerConfig, datepickerPopupConfig) {

Karma yells at me because I am not properly mocking $routeProvider, datepickerConfig, etc with the following:

Error: [$injector:modulerr] Failed to instantiate module foo due to:
Error: [$injector:unpr] Unknown provider: $routeProvider

(and then if I remove $routeProvider then it says Unknown provider: datepickerConfig and so on)

I also have the following code in a beforeEach:

angular.mock.module('foo');
angular.mock.module('ngRoute');
angular.mock.module('ui.bootstrap');

And the following in my karma.conf.js:

  'components/angular/angular.js',
  'components/angular/angular-mocks.js',
  'components/angular/angular-route.js',
  'components/angular-ui/ui-bootstrap-tpls.js',
  'app/*.js', // app code
  'app/**/*.js',
  'app/**/**/*.js',
  'test/app/*.js', // app.js
  'test/specs/*.js', // angular.mock.module calls
  'test/**/*.js', // tests
  'test/**/**/*.js'

Thank you for any advice.

like image 355
Alex Crooks Avatar asked Nov 10 '22 05:11

Alex Crooks


1 Answers

Make sure to include the angular-route module and all your dependencies into the flies array of your karma.conf.js. That should do the trick.

like image 119
Ciro Nunes Avatar answered Nov 15 '22 12:11

Ciro Nunes