小编oez*_*any的帖子

如何在单元测试时在角度1.5中添加$ componentController的依赖项

我想在组件中向Controller添加依赖项:

这是我的组件:

angular
  .module('app')
  .component('myComponent', {
    template: '<div>some content</div>',
    controller: ["$routeParams", function ($routeParams) {
       var ctrl = this;
       ctrl.myId = $routeParams.id;
  });
Run Code Online (Sandbox Code Playgroud)

现在我只想测试如下:

describe("spec for myComponent", function () {

  var $componentController;
  var $routeParams;

  beforeEach(module('app'));
  beforeEach(inject(function (_$componentController_, _$routeParams_) {
    $componentController = _$componentController_;
    $routeParams = _$routeParams_;
    $routeParams = {
      myId: 42
    }
  }));

  it('should init', function () {

    var ctrl = $componentController('myComponent', $routeParams, null);

    expect(ctrl.myId).toBe(42);

  });
});
Run Code Online (Sandbox Code Playgroud)

但遗憾的是ctrl.myId未定义,因为没有正确注入$ routeParams.为什么?

testing dependencies components angularjs

5
推荐指数
0
解决办法
3117
查看次数

标签 统计

angularjs ×1

components ×1

dependencies ×1

testing ×1