如何在角度测试中对dom操纵函数进行单元测试

时间:2017-04-25 17:45:42

标签: unit-testing angular typescript

我有一个基于角度cli的项目,其组件具有如下逻辑:

  colorLine() {
      let svgDoc = this.svg.nativeElement.contentDocument;
      JSON.parse(this.stations).forEach(station => {
        station.element = svgDoc.getElementById(station.name);
          station.element.removeAttribute('class');
          if (station.status === 'running') {
            station.element.setAttribute('fill', 'lime');
          }
        return station;
      })
    }

我正在尝试使用以下测试进行单元测试:

  it('should color running stations green', async(() => {
    component.svg.nativeElement = new DOMParser().parseFromString(svgDoc, 'application/xml');

    component.load();
    expect(component.stations[0].element.getAttribute('fill')).toBe('lime');
  }));

其中svgDoc是要查询的元素的字符串表示形式。但是,我遇到了这个问题:

  

TypeError:无法读取未定义

的属性'getAttribute'

如何测试此类功能?

0 个答案:

没有答案
相关问题