无法读取未定义业力茉莉花的属性“订阅”

时间:2021-07-20 18:33:39

标签: angular karma-jasmine angular2-services angular-test

我需要创建这个 get 的测试,但每次我尝试时都会收到错误“TypeError:无法读取未定义的属性‘订阅’”

我的服务功能

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

}

我的规格

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();

})

1 个答案:

答案 0 :(得分:0)

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

尝试将此行更改为:

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

其他看起来不错。