【问题标题】:Cannot read property 'subscribe' of undefined karma jasmine无法读取未定义业力茉莉花的属性“订阅”
【发布时间】:2021-09-28 06:09:22
【问题描述】:

我需要为此获取创建一个测试,但每次尝试我都会收到错误“TypeError: Cannot read property 'subscribe' of undefined”

我的服务功能

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

}

我的规格.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();

})

【问题讨论】:

  • 如果您将使用returnValue 返回的内容与documentation 返回的内容进行比较,您会发现哪些主要区别?
  • 同时展示你是如何在测试中设置modalIndicationService,以及getAPICard是如何实现的。

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


【解决方案1】:

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

尝试将此行更改为:

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

其余的看起来不错。

【讨论】:

  • 你是我的英雄,但是当我这样做并且它变成绿色时,是否意味着我的测试已经完成?
猜你喜欢
  • 2019-04-06
  • 1970-01-01
  • 1970-01-01
  • 2018-12-08
  • 2013-11-14
  • 1970-01-01
  • 1970-01-01
  • 2014-12-12
  • 2013-10-17
相关资源
最近更新 更多