在经历了类似的话题之后,仍然不知道我的单元测试出了什么问题。运行会导致标题中所述的错误。
测试设置:
按价值提供的模拟服务
export class MockedDrawingService {
public drawing = {} as Drawing;
constructor() {}
add(_server: Server, _project_id:string, _x: number, _y:number, _svg: string) {
return of(this.drawing);
}
}
正在测试:
it('should add selected drawing', () => {
//some code here
spyOn(drawingService, 'add');
component.addDrawing("rectangle");
dummyElement.click();
expect(drawingService.add).toHaveBeenCalled();
});
在组件中:
this.drawingService
.add(this.server, this.project.project_id, x, y, svg)
.subscribe((serverDrawing: Drawing) => {
this.drawingsDataSource.add(serverDrawing);
});
带有订阅的行会导致异常:无法读取未定义的属性“ subscribe”