测试使用Jasmine和Karma是否更改了班级

时间:2019-12-02 12:34:37

标签: angular unit-testing jasmine karma-jasmine angular-unit-test

我正在尝试通过以下方式测试是否单击了按钮:

单击按钮时,某些元素类从wrapper-container更改为wrapper-container-expanded(我不想检查单击按钮时是否调用了函数-这不是我的目的)

我正尝试通过以下方式对此进行检查:

it('should class be changed', ()=>{ 
   fixture.detectChanges()
   clickedElement.nativeElement.click() // click the button that causes the class change
   elementToBeExpanded = fixture.debugElement.nativeElement.querySelector('wrapper-container-expanded')
   expext(elementToBeExpanded).toBeTruthy() // check if the element with the new class exists in the DOM -the elementToBeExpanded is null
})

尽管我可以看到调用了函数并且更改了类,但是找不到具有新类的元素,并且旧的wrapper-container似乎存在。

1 个答案:

答案 0 :(得分:4)

点击事件后,您需要致电 fixture.detectChanges()

it('should class be changed', () => { 
   // ...
   clickedElement.nativeElement.click() // click the button that causes the class change
   fixture.detectChanges()              // detect the changes!!
   // ...
})