代码之家  ›  专栏  ›  技术社区  ›  RV.

如何用DIV作为viewChild测试角度分量

  •  1
  • RV.  · 技术社区  · 6 年前

    我有一个角分量,如下图所示。我想为下面的initmychart()函数编写jasmine测试。我无法初始化库,因为我在测试中没有可访问的viewchild div元素。

    component.html
    <div #myId></div>
    
    component.ts
    export class MyComponent implements OnInit{
    
      chart: ChartLibrary;
    
      @ViewChild('myId') myIdDiv: ElementRef
    
      constructor(){}
    
      initMyChart(){
       //Using some library which takes the div as input to initialize
       this.chart = new Chart(this.myIdDiv.nativeElement)
      }
    }
    

    当我尝试运行测试时,会出现以下错误

    console.error node_modules\jsdom\lib\jsdom\virtual-console.js:29
        Error: Not implemented: HTMLCanvasElement.prototype.getContext (without installing the canvas npm package)
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   Akatsuki    6 年前

    我想可以用fixture.debuglement。例如:

    const el = fixture.debugElement.nativeElement.querySelector('#myId');
    

    您可以通过以下链接: enter link description here .