PRIME ng确认服务的角度单元测试

apo*_*tsi 4 service unit-testing primeng angular

首先,我是角单元测试的新手。我想对以下方法进行单元测试,该方法将从我的数据中删除一条记录。方法是:

//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: () => {       
      }
      });

  }
Run Code Online (Sandbox Code Playgroud)

如您所见,我正在从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();
});
Run Code Online (Sandbox Code Playgroud)

因此,我正在调用该服务以加载我的数据(dataService),然后调用我的方法以删除第一条记录。但是什么也没发生。单元测试成功完成,但未删除任何数据。

有什么想法吗?

Jur*_*uri 7

上primeng > 7.1

spyOn(confirmationService, "confirm").and.callFake((confirmation: Confirmation) => confirmation.accept());
Run Code Online (Sandbox Code Playgroud)


blo*_*ish 5

您可以使用茉莉花伪造品来覆盖确认对话框并按如下所示调用accept函数

spyOn(confirmationService, 'confirm').and.callFake((params: any) => {
      console.log(`fake calling accept`);
      params.accept();
})
Run Code Online (Sandbox Code Playgroud)