我正在按角度编写按钮单击事件的单元测试。下面提到的按钮单元测试在功能未参数化的情况下可以很好地工作-
如果可以的话,它将起作用:
(click)="editForms()"
但是当我在函数中添加参数时,就像这样:
(click)="editForms(array) , the this test fails as - "Failed: Cannot read property 'click' of null" --
它无法调用函数editForms
。
有人可以告诉我该怎么做吗?
beforeEach(() => {
fixture = TestBed.createComponent(MyjobsComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
fit('should do editForms button click ', async(() => {
spyOn(component, 'editForms');
let button = fixture.debugElement.nativeElement.querySelector('button');
button.click();
fixture.whenStable().then(() => {
expect(component.editForms).toHaveBeenCalled();
})
}));
HTML:
<button mat-raised-button style="background-color:#6666ff;color:white;" (click)="editForms(array)"> <i class="material-icons">create</i></button>
component.ts:
editForms(name) {
console.log(name);
console.log( this.totalJobData);
for(let e = 0; e < this.totalJobData.length; e++){
if(this.totalJobData[e].jobName === name){
this.jobID = this.totalJobData[e].jobId;
this.sourceCode = this.totalJobData[e].sourceCode;
}`enter code here`
}
console.log(this.sourceCode);
.....