【发布时间】:2020-03-27 01:44:36
【问题描述】:
我正在尝试通过以下方式测试按钮是否被点击:
单击按钮时,某些元素类从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似乎存在。
【问题讨论】:
-
对于组件状态和简单操作,我建议查看 Angular 测试库。对于这种情况,它是一个很棒的工具。 github.com/testing-library/angular-testing-library
标签: angular unit-testing jasmine karma-jasmine angular-unit-test