角度单位测试中的未知提供商

时间:2014-10-29 10:06:25

标签: angularjs

我有这个控制器:

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

和这个测试:

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说:

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

但在我的karma.config中,我正在导入bower_components/angular-route/angular-route.js。这里:

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
   });
};

我有点疑惑。任何人吗?

1 个答案:

答案 0 :(得分:4)

您可以通过&#39; $ routeParams&#39;反对$controller调用,它看起来像这样:

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