如何在rxjs链中测试功能

时间:2019-07-16 08:32:34

标签: angular testing dialog angular-material

我正在尝试测试mat-dialog关闭后发生的回调。我的代码显示为rxjs链,我需要测试该链中的一些功能

这是带有Angular材质的Angular 7

要测试的代码


deleteProject() {
    this.dialog.open(DeleteProjectDialogComponent, {
      data: {
        project: this.project
      },
      minWidth: MIN_DIALOG_WIDTH
    })
      .afterClosed()
      .pipe(
        take(1),
        filter(Boolean),
        switchMap(() => this.apiProjects.deleteProject(this.project.id)), // <= need to test this call of this function
        withLatestFrom(this.translate.get('PROJECT.WAS_DELETED')),
        tap({
          next: ([res, translation]) => {
            this.utils.storage.clearProject();
            this.utils.navigation.goToAuthPage();
            this.snackBar.open(translation, 'OK', {
              duration: SNACK_BAR_DURATION
            });
          },
          error: res => {
            const {error} = res;
            if (res.status === 400) {
              const {name, project} = error;
              if (project) {
                this.showError(error.project);
              }
            }
          }
        })
      ).subscribe();
  }

我的测试


it('should call api after delete project', () => {

    const spyApi = spyOn(apiProject, 'deleteProject');
    spyOn(dialog, 'open').and.returnValue({
      afterClosed: () => of(true)
    });

    component.deleteProject();

    expect(spyApi).toHaveBeenCalled(); // <= failed with Error: Expected spy deleteProject to have been called.
  });

0 个答案:

没有答案