为HttpClient post创建spy会创建一个返回值错误



我有一个以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({}));

相关内容

  • 没有找到相关文章