我正在为click事件编写业力/茉莉花测试用例,但按钮却为空。请给我建议。预先感谢。
it('should', async(() => {
spyOn(component, 'clickMethod');
let button = fixture.debugElement.nativeElement.querySelector('#view-rec');
console.log(button); --> button is null I am getting
button.click();
fixture.whenStable().then(() => {
expect(component.clickMethod).toHaveBeenCalled();
})
component ::
clickMethod() {
this.msg.node.setVal(false);
this.myBtn = "Hello";
}
HTML ::
<mat-menu #menu="matMenu" class="dropdown">
<button mat-menu-item>
</button>
<button mat-menu-item id="view-rec"(click)='clickMethod()'>
</button>
</mat-menu>
答案 0 :(得分:1)
我认为您需要先打开菜单1,然后才能访问要单击的mat-menu-item
。
例如:
<button id="menu-open-icon" mat-icon-button [matMenuTriggerFor]="menu">
<mat-icon>more_vert</mat-icon>
</button>
<mat-menu #menu="matMenu">
<button mat-menu-item id="view-rec"(click)='clickMethod()'>
<mat-icon>dialpad</mat-icon>
<span>Redial</span>
</button>
<button mat-menu-item disabled>
<mat-icon>voicemail</mat-icon>
<span>Check voicemail</span>
</button>
<button mat-menu-item>
<mat-icon>notifications_off</mat-icon>
<span>Disable alerts</span>
</button>
</mat-menu>
尝试:
it('should', () => {
spyOn(component, 'clickMethod').and.callThrough();
let menuIcon = fixture.debugElement.nativeElement.querySelector('#menu-open-icon')
menuIcon.click();
let button = fixture.debugElement.nativeElement.querySelector('#view-rec');
console.log(button); --> this should not be null now
button.click();
fixture.detectChanges();
expect(component.clickMethod).toHaveBeenCalled()
})