尝试单元测试角度1.5组件时接收未知的提供程序错误

时间:2017-07-21 14:24:35

标签: angularjs unit-testing jasmine angular-components grunt-contrib-jasmine

所以我正在测试我用Angular 1.5制作的组件。出于安全考虑,我不能在这里复制和粘贴该代码,但我可以给出一些模拟。

这是一个简单的组件,它具有一些基本的选择操作,在选择某些内容时具有绑定事件。

所以代码看起来像这样:

angular
      .module( "myModule" )
      .component( "myComponent", { 
       template: "Some random HTML Template",
       bindings: { 
         onSelect: "&" 
       },
       controller: function () {
        var ctrl = this;
        ctrl.$onInit = init;
        ctrl.selection = selection; 
        function init() {}  function selection() { 
             ctrl.onSelect(ctrl.selected) } 
       } } ); 

这是基本组成部分。我正在尝试对其进行单元测试,格式如下:

describe( "Component Test", function() { 
    beforeEach (module( "myModule") ); 
    var ctrl, onSelectSpy;
    beforeEach (inject( function( _$componentController_) { 
    ctrl = {}
    onSelectSpy = jasmine.createSpy("onSelect");
    var $componentController = _$componentController_;
    ctrl = $componentController("myComponent", null, { onSelect: 
onSelectSpy}); 
    } ) ); 

    it ("Controller defined", function() { 
    ctrl.$onInit(); 
   expect ( ctrl ).toBeDefined(); 
  }); 
} ); 

当我尝试运行看起来与此非常相似的测试时,我收到此错误:

未知提供者:myComponentDirectiveProvider< - myComponentDirective 它给出了一个关于它失败的线的角度网址。

不确定为什么没有定义以及为什么这不起作用。我认为组件是指令。

使用Angular 1.5.8和Angular-mocks 1.5.8

0 个答案:

没有答案
相关问题