Javascript 使用';控制器组件';语法(此选项而不是$scope)

Javascript 使用';控制器组件';语法(此选项而不是$scope),javascript,angularjs,unit-testing,dependency-injection,karma-jasmine,Javascript,Angularjs,Unit Testing,Dependency Injection,Karma Jasmine,我正在尝试设置karma来进行一些单元测试,但我无法在一个基本示例上实现它: 这是控制器文件: angular.module('balrogApp.requests', [ /* Dependancies */ ]) // Routes configuration .config(['$routeProvider', function($routeProvider) { /* $routeProvider configuration */ }]) .controlle

我正在尝试设置karma来进行一些单元测试,但我无法在一个基本示例上实现它:

这是控制器文件:

angular.module('balrogApp.requests', [
  /* Dependancies */
])
  // Routes configuration
  .config(['$routeProvider', function($routeProvider) {
    /* $routeProvider configuration */
  }])
  .controller('requestsController', function(Requests, Users, Projects, RequestsComments, CostEstimations,
                                             Regions, growl, $route, $rootScope, $scope, $location) {
    this.test = 'test42';

/* ... */

});
这是测试文件:

describe('requestsController', function() {
  beforeEach(module('balrogApp.requests'));

  var ctrl;
  beforeEach(inject(function($controller) {
    ctrl = $controller('requestsController');
  }));

  it('should have test = "test42"', function(){
    expect(ctrl.test).toBe("test42");
  });
});
但会抛出此错误:

Chrome 43.0.2357(Windows 7 0.0.0)请求控制器应进行测试 =“test42”失败 错误:[$injector:unpr]未知提供程序:$scopeProvider控制器所依赖的
$scope
)是所谓的“本地”,即它特定于控制器实例,不存在于依赖项注入容器中。您必须自己提供,例如:

beforeEach(inject(function($rootScope, $controller) {
    ctrl = $controller('requestsController', { $scope: $rootScope.$new() });
}));
因为您正在创建一个新的作用域,所以最好在测试后销毁它:

var scope;

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

afterEach(function() {
    scope.$destroy();
});