Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 启动和模拟确认服务_Angular_Mocking_Primeng - Fatal编程技术网

Angular 启动和模拟确认服务

Angular 启动和模拟确认服务,angular,mocking,primeng,Angular,Mocking,Primeng,我在尝试模拟启动确认服务时遇到问题,因此我可以测试accept函数。 在我的组件上有以下方法 deleteRow(rowData: any): void{ this.confirmationService.confirm({ message: 'Are you sure that you want to perform this deletion?', accept: (): void => { this.messageSe

我在尝试模拟启动确认服务时遇到问题,因此我可以测试accept函数。 在我的组件上有以下方法

deleteRow(rowData: any): void{

      this.confirmationService.confirm({
        message: 'Are you sure that you want to perform this deletion?',
        accept: (): void => {
          this.messageService.add({
            severity: 'success',
            summary: `Deleted`,
            detail: `Deleted ${rowData.name}`
          });

          const index: number = this.areas.indexOf(rowData);
          this.areas.splice(index, 1);
        }
    });
  }
下面的测试

    const confirmationService: ConfirmationService = TestBed.get(ConfirmationService);

    const mockConfirm: any = spyOn(confirmationService, 'confirm').and.callFake((c: any) => {
      c.accept();
    });
    component.deleteRow({id: 'aaa'});

    expect(mockConfirm).toHaveBeenCalled();
    
  });
这是我的试验台设置

  beforeEach(async(() => {
      TestBed.configureTestingModule({
        schemas: [NO_ERRORS_SCHEMA],
        declarations: [AreasListComponent],
        imports: [FormsModule,
          ReactiveFormsModule,
          ConfirmDialogModule,
          FieldsetModule,
          TableModule,
          ToastModule,
          DropdownModule,
          NoopAnimationsModule
        ],
        providers: [
          ConfirmationService,
          MessageService
        ]
      })
        .compileComponents();
    })
  );
mockconfirm
tohaveincalled
失败,当我调试它时,服务上似乎没有mock

有人有什么想法吗


关于

问题似乎在于,
确认
方法位于
确认服务
原型上,而不是对象本身。当您监视
confirm
方法时,您监视的是特定于实例的方法,因此它不起作用

尝试以下代码,它应该可以工作:

const mockConfirm: any = spyOn(ConfirmationService.prototype, 'confirm').and.callFake((c: any) => {
  c.accept();
});

您可以
控制台.log
或调试假回调以查看是否正在调用它吗?您好,谢谢您为我指明了正确的方向。我必须使用const mockConfirm:any=spyOn(confirmationService.uu proto_uu,'confirm')和.callFake((c:any)=>{c.accept();})