无法读取未定义的业力茉莉花的 属性 'subscribe'

Cannot read property 'subscribe' of undefined karma jasmine

我需要为此 get 创建一个测试,但每次我尝试时都会收到错误“TypeError:无法读取未定义的 属性 'subscribe'”

我的服务功能

getAPICard(): Observable<any> {
return this.http.get<any[]>(`${this.GET_APICard}`);

}

我的spec.ts

it('testing get api', (done:DoneFn) =>{
  const mockObj = [{id:'1',
   title:'title', 
   description:'description',
   image: 'imagem',
   amount: 10
  }];

  httpClientSpy.get.and.returnValue(of(mockObj)).and.callThrough();

 modalIndicationService.getAPICard().subscribe(
   dados => {
    expect(dados).toEqual(mockObj, 'mockObj');
    done()
   },
   done.fail
 )
  
  expect(httpClientSpy.get.calls.count()).toBe(1, 'one call');
  done();

})

我认为你不能同时拥有 .and.returnValue.and.callThrough()。两者中只有一个有意义,因为 returnValue 将始终 return 一个值,而 callThrough 将调用实际函数。

尝试将此行更改为:

httpClientSpy.get.and.returnValue(of(mockObj));

其他看起来不错