首先,我是角度单元测试的新手。我想对以下从数据中删除记录的方法进行单元测试。方法是:
//Confirm Button for deletion
confirm(name: string, id: any) {
this.confirmationService.confirm({
message: 'Are you sure you want to remove ' + name + ' from your list of Supporting Staff?',
accept: () => {
const index: number = this.data.indexOf(id);
if (index !== -1) {
this.data.splice(index,1);
this.totalResults = this.data.length;
this.updateVisibility();
this.alertMessage = { severity: 'success', summary: 'SUCCESSFUL REMOVAL', detail: 'You have successfully removed '+name+' from your Supporting Staff List.' };
this.alertMessagesSrv.pushAlert(this.alertMessage);
}
},
reject: () => {
}
});
}
正如您所看到的,我正在调用 PRIME ng 的确认服务,并打开一个对话框询问用户是否要删除所选记录。 (数据是我的记录)。
这是我的单元测试:
it('should remove a supporting staff from list', () => {
let fixture = TestBed.createComponent(SupportingStaffComponent);
let app = fixture.debugElement.componentInstance;
let dataService = fixture.debugElement.injector.get(ProvidersService);
let spy = spyOn(dataService,'getSupportingStaffList').and.callThrough();
fixture.detectChanges();
let confirmFunction = fixture.componentInstance.confirm(app.data[0].name,1);
let confirmService = fixture.debugElement.injector.get(ConfirmationService);
//fixture.nativeElement.querySelector('#btnYes').click();
let spyRemove = spyOn(confirmService,'accept').and.callThrough();
fixture.detectChanges();
console.log(app.data);
expect(app.data).toBeDefined();
});
因此,我调用服务来加载我的数据(dataService),然后调用我的方法来删除第一条记录。但什么也没发生。单元测试成功完成,但没有删除任何数据。
有什么想法吗?