我有一个将HttpClient服务作为值的服务,我想监视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); });});
1 回答
狐的传说
TA贡献1804条经验 获得超3个赞
您正在返回一个函数,该函数返回一个 Observable 而不仅仅是一个 Observable
postSpy = spyOn(httpClient, 'post').and.returnValue(new Subject<any>().asObservable());
或者
postSpy = spyOn(httpClient, 'post').and.returnValue(of({}));
添加回答
举报
0/150
提交
取消