Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
405 views
in Technique[技术] by (71.8m points)

angular - Jasmine spy return type is different from when method is called from a service

I have an overloaded function with multiple return types

getSettings(contractId: string, observe?: 'body'): Observable<ResponseV1>;
getSettings(contractId: string, observe?: 'response'): Observable<HttpResponse<ResponseV1>>;
getSettings(contractId: string, observe?: 'events'): Observable<HttpEvent<ResponseV1>>;

When I call it in a service, it returns the expected type which would be the default

getMarketSettings(contractId): Observable<ResponseV1> {
    return this.service.getSettings(contractId)
}

However, when I try to test getMarketSettings

it('fetch settings', (done) => {
   const returnValue : ResponseV1 = {Some data ...}

   // This following line complains that the return type of the spy is Observable<HttpEvent<ResponseV1>>
   const spyOnService = spyOn(apiService, 'getSettings').and.returnValue(of(returnValue))

   service.getMarketSettings('').subscribe((result) => {
      expect(result).toEqual(returnValue)
      expect(spyOnService).toHaveBeenCalledWith('')
      done()
    })
  })

Any idea why the spy has a different return type from when the method is called in the service?

I can solve the problem by adding // @ts-ignore to ignore the incompatible types, but I still dont understand why it is happening.

question from:https://stackoverflow.com/questions/65883970/jasmine-spy-return-type-is-different-from-when-method-is-called-from-a-service

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...