我有一个以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);
});
});
您返回的函数返回Observable,而不是仅返回Observable
postSpy = spyOn(httpClient, 'post').and.returnValue(new Subject<any>().asObservable());
或
postSpy = spyOn(httpClient, 'post').and.returnValue(of({}));