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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…