Angular 中嵌套 api 调用的单元测试用例

问题描述 投票:0回答:0

我正在尝试为嵌套 API 调用编写单元测试用例,但不知道如何为以下场景编写单元测试用例。

this.openService
      .createDocument(PIReq, this.id)
      .subscribe({
        next: (res1) => {
          const applicationReq = AppUtil.getApplicationRequest(
            this.openService.personDetails,
            res1.id
          );
          this.openService
            .createPersonAppliation(applicationReq, this.id)
            .subscribe({
              next: (res2) => {
                const personAppReqInfo: PersonApplicationIds = {
                  documentId: res1.id,
                  personApplicationId: res2.id,
                };
                
                if (this.openDataStoreService.details.length > 0) {
                  this.submitApplicationRequest();
                  this.invokeRequest();
                } else {
                  this.invokeRequest();
                }
              },
              error: () => {
                this.showSpinner = false;
              },
            });
        },
        error: () => {
          this.showSpinner = false;
        },
     });

这是我试过的

let sampleApplicationResponse!: ApplicationResponse;
jest.spyOn(openService, 'createPersonAppliation').mockReturnValueOnce(of(sampleApplicationResponse));
    openService.createPersonAppliation(applicationReq, component.id)
      .subscribe({
        next: applicationRes => {
          expect(applicationRes).toBe(sampleApplicationResponse);
        }
      });

这个测试用例有效,但问题是这个测试用例对覆盖范围没有任何贡献,有什么方法可以测试嵌套api调用中的所有行? Jest/Jasmine 什么都好

angular unit-testing jestjs jasmine
© www.soinside.com 2019 - 2024. All rights reserved.