带笑话的角度单元测试:订阅不是函数

时间:2020-03-25 07:52:44

标签: angular jestjs

我有服务:

  getNode(name: string): Observable<nodes> {
    return this.get(`${this.rootUrl}/names/${name}`);
  }

组件调用

  ngOnChanges() {
    if (this.selectedStructure) {
      this.nodeSvs.getNode(this.selectedStructure)
      .subscribe(tree => {
        ...
      });
    }
  }

我的考试

  const mockNodeService = {
    getNode: jest.fn()
  };
  mockNodeService.getNode.mockReturnValue(of(bustree));

  configureTestSuite(() => {
    TestBed.configureTestingModule({
      ...,
      providers: [{ provide: NodeService, useValue: mockNodeService }]
    }).compileComponents();
  });

it('should return bus tree', () => {
  component.selectedStructure = 'abc';
  component.ngOnChanges();

  expect(mockNodeService.getNode).toHaveBeenCalledWith(component.selectedStructure);
});

测试时,我得到TypeError: this.nodeSvs.getNodeBusTree(...).subscribe is not a function,但组件运行正常。

0 个答案:

没有答案
相关问题