Karma jasmine和角度控制器使用'Controller as'语法(这取代$ scope)

时间:2015-11-17 16:32:49

标签: javascript angularjs unit-testing dependency-injection karma-jasmine

我正在设置业力来进行一些单元测试,但我无法让它在一个基本的例子上工作:

这是控制器文件:

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)requestsController应该有测试   =“test42”失败           错误:[$ injector:unpr]未知提供者:$ scopeProvider< - $ scope< - requestsController           http://errors.angularjs.org/1.4.7/ $注射器/ unpr?P0 =%24scopeProvider%20%3 C-%20%24scope%20%3 C-%20requestsController               在C:/Users/aazor102115/Desktop/Dev/Balrog/bower_components/angular/angular.js:68:12               在C:/Users/aazor102115/Desktop/Dev/Balrog/bower_components/angular/angular.js:4289:19               at Object.getService [as get](C:/Users/aazor102115/Desktop/Dev/Balrog/bower_components/angular/angular.js:4437:39)               在C:/Users/aazor102115/Desktop/Dev/Balrog/bower_components/angular/angular.js:4294:45               at getService(C:/Users/aazor102115/Desktop/Dev/Balrog/bower_components/angular/angular.js:4437:39)               在调用时(C:/Users/aazor102115/Desktop/Dev/Balrog/bower_components/angular/angular.js:4469:13)               at Object.instantiate(C:/Users/aazor102115/Desktop/Dev/Balrog/bower_components/angular/angular.js:4486:27)               在C:/Users/aazor102115/Desktop/Dev/Balrog/bower_components/angular/angular.js:9151:28               在C:/Users/aazor102115/Desktop/Dev/Balrog/bower_components/angular-mocks/angular-mocks.js:1882:12               在对象。 (C:/Users/aazor102115/Desktop/Dev/Balrog/tests/requests.js:6:12)           错误:声明位置               在window.inject.angular.mock.inject(C:/Users/aazor102115/Desktop/Dev/Balrog/bower_components/angular-mocks/angular-mocks.js:2409:25)               在套房。 (C:/Users/aazor102115/Desktop/Dev/Balrog/tests/requests.js:5:14)               在C:/Users/aazor102115/Desktop/Dev/Balrog/tests/requests.js:1:1           TypeError:无法读取undefined的属性'test'               在对象。 (C:/Users/aazor102115/Desktop/Dev/Balrog/tests/requests.js:10:16)   Chrome 43.0.2357(Windows 7 0.0.0):执行1 of 1(1 FAILED)ERROR   (0.108秒/​​0.087秒)

1 个答案:

答案 0 :(得分:2)

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