使用 Angular 对 CSS 进行单元测试

phy*_*boy 3 html css testing angular

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

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

IE:

<div #myDiv>Hello world</div>
Run Code Online (Sandbox Code Playgroud)
@ViewChild('myDiv') myDiv: ElementRef;

myFunct() {
  this.myDiv.nativeElement.style.height = 500;
}
Run Code Online (Sandbox Code Playgroud)
it('should increase div size', () => {
  // Here is where I get stuck...
});
Run Code Online (Sandbox Code Playgroud)

更新

实施测试,例如:

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

    expect(component.dropdown.nativeElement.style.height).toBe(34);
  });
Run Code Online (Sandbox Code Playgroud)

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

预计 '' 为 500。

小智 6

像这样的东西...

  1. 公开暴露 myDiv

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

  3. 添加

component.myFunct();

expect(component.myDiv.nativeElement.style.height).toBe(500);
Run Code Online (Sandbox Code Playgroud)