3

我有以下方法

protected create (): void {
    this.projectService.getPartsList(this.project.id, this.project.versionId).subscribe((geometry: Geometry) => {
      this.projectService.createNewProject({
        data: {
          parts: [ geometry.data.parts ]
        },
        importedVersions: [{
          assetClass: '3dgeometry',
          versionId: this.project.versionId
        }],
        modelOfferingId: this.project.modelOfferingId,
        name: this.projectName
      });
    });
  }

和以下单元测试:(注意 ProjectService 正在使用一个模拟类,它为其中的所有方法返回 spys)

  describe('create()', () => {
    it('should call getPartsList from projectService and then call createNewProject when the resolves passing the payload', () => {
      let payload: any = {
        data: {
          parts: []
        },
        importedVersions: [{
          assetClass: '3dgeometry',
          versionId: ''
        }],
        modelOfferingId: '',
        name: 'Test Project'
      };
      modalCreateAdvancedInstance.projectName = payload.name;
      modalCreateAdvancedInstance.project = {
        modelOfferingId: '',
        versionId: ''
      };
      modalCreateAdvancedInstance.create();
      expect(projectService.getPartsList).toHaveBeenCalled();
      expect(projectService.createNewProject).toHaveBeenCalledWith(payload);
    });
  });

createNewProject方法永远不会被调用,因为该getPartsList方法只返回一个间谍并且不调用.subscribe(). 我如何模拟这个,以便 getPartsList 返回一个调用 .subscribe 的 observable。

4

0 回答 0