首页 > 解决方案 > 带角度的单元测试css

问题描述

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

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

IE:

<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。

标签: htmlcssangulartesting

解决方案


像这样的东西...

  1. 公开暴露 myDiv

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

  3. 添加

component.myFunct();

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

推荐阅读