为 HttpClient post 创建间谍会产生 return 值错误

Creating spy for HttpClient post creates a return value error

我有一项服务将 HttpClient 服务作为值,我想监视该 class 的 post 方法,所以我创建了一个看起来像这个:

spyOn(httpClient, 'post').and.returnValue(() => new Subject<any>().asObservable());

但是,我收到以下错误:

Argument of type '() => Observable<any>' is not assignable to parameter of type 'Observable<unknown>'.
  Type '() => Observable<any>' is missing the following properties from type 'Observable<unknown>': _isScalar, source, operator, lift, and 6 more.

这是它来自的规范:

describe('GraphQLClientService', () => {
  let service: GraphQLClientService;
  let httpClient: HttpClient;
  let postSpy: any;

  beforeEach(() => {
    httpClient = jasmine.createSpyObj('HttpClient', []);
    postSpy = spyOn(httpClient, 'post').and.returnValue(() => new Subject<any>().asObservable());
    service = new GraphQLClientService(httpClient);
  });
});

您返回的函数 returns 是一个 Observable 而不仅仅是一个 Observable

postSpy = spyOn(httpClient, 'post').and.returnValue(new Subject<any>().asObservable());

postSpy = spyOn(httpClient, 'post').and.returnValue(of({}));