【问题标题】:Karma jasmine and angular controller using 'Controller as' syntax (this instead of $scope)使用“Controller as”语法的 Karma jasmine 和角度控制器(这个而不是 $scope)
【发布时间】:2016-02-19 03:16:16
【问题描述】:

我正在尝试设置 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) requestsController 应该有测试 =“test42”失败 错误:[$injector:unpr] 未知提供者:$scopeProvider http://errors.angularjs.org/1.4.7/$injector/unpr?p0=%24scopeProvider%20%3C-%20%24scope%20%3C-%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 在 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 在 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) 在 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:无法读取未定义的属性“测试” 在对象。 (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) 错误 (0.108 秒 / 0.087 秒)

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-13
      • 1970-01-01
      • 2016-09-20
      • 2014-10-08
      • 2015-08-04
      • 2020-12-07
      相关资源
      最近更新 更多