1

因此,我正在对使用 Angular 1.5 制作的组件进行单元测试。出于安全考虑,我无法在此处复制和粘贴该代码,但我可以提供一些模拟。

It's a simple component that has some basic selection action, with an binding event when something is selected.

所以代码看起来像这样:

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 它给出了一个关于它失败的行的角度 url。

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

使用 Angular 1.5.8 和 Angular-mocks 1.5.8

4

0 回答 0