我有以下方法
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。