【问题标题】:Unit testing css with angular带角度的单元测试css
【发布时间】:2018-10-12 14:46:31
【问题描述】:

有什么方法可以测试元素的height css 属性吗?

我有一个函数可以改变div 的高度,如果能在测试中检查该函数是否正确运行会很好...

即:

<div #myDiv>Hello world</div>

@ViewChild('myDiv') myDiv: ElementRef;

myFunct() {
  this.myDiv.nativeElement.style.height = 500;
}

it('should increase div size', () => {
  // Here is where I get stuck...
});

更新

实施如下测试:

it('should return correct height of dropdown when initialised', () => {
    component.myFunct();

    expect(component.dropdown.nativeElement.style.height).toBe(34);
  });

导致测试失败并显示消息:

预计 '' 为 500。

【问题讨论】:

    标签: html css angular testing


    【解决方案1】:

    这样的……

    1. 公开 myDiv

    2. 为组件设置测试台(通常默认添加)

    3. 添加

    component.myFunct();
    
    expect(component.myDiv.nativeElement.style.height).toBe(500);
    

    【讨论】:

    • 在发布问题之前,我已经做了类似的事情。我这样做时遇到的问题是我失败并返回,Expected '' to be 34....
    • 啊我明白现在发生了什么。我在nativeElement &gt; style &gt; heightnativeElement &gt; clientHeight 之间混淆了自己
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-10
    • 2018-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-24
    • 1970-01-01
    相关资源
    最近更新 更多