gpt4 book ai didi

typescript - Ionic + Jasmine + Tslint - 类型上不存在属性 'and'

转载 作者:行者123 更新时间:2023-12-03 15:25:09 25 4
gpt4 key购买 nike

大家。我正在构建一个 Ionic 应用程序 boilerplate .
现在,我正在为特定页面构建测试,我正在使用 jasmine 来模拟提供程序并设置方法的预期行为。
就是这样的beforeEach() 方法看起来像:

    beforeEach(() => {
mockLoadingController = jasmine.createSpyObj('ModalController', ['create', 'present', 'dismiss']);
mockLoadingController.create.and.returnValue(mockLoadingController);

mockModalController = jasmine.createSpyObj('LoadingController', ['create', 'present',
'onDidDismiss', 'dismiss']);
mockModalController.create.and.returnValue(mockModalController);

mockGeolocation = jasmine.createSpyObj('Geolocation', ['getCurrentPosition']);

mockGeolocation.getCurrentPosition.and.callFake(( ) => {
return {then: ( ) => { }};
});
mockEvents = jasmine.createSpyObj('Events', ['publish', 'subscribe']);
TestBed.configureTestingModule({
schemas: [CUSTOM_ELEMENTS_SCHEMA],
providers: [
{ provide: NavController, useValue: mockNavController },
{ provide: LoadingController, useValue: mockLoadingController },
{ provide: ModalController, useValue: mockModalController },
{ provide: Geolocation, useValue: mockGeolocation },
{ provide: Events, useValue: mockEvents },
LocationPage,
],
},
); } );

当我为 定义 promise 返回时,问题就开始了。获取当前位置 方法:
    mockGeolocation.getCurrentPosition.and.callFake(( ) => {
return {then: ( ) => { }};
});

我在测试时使用 tslint,它给了我以下错误
ERROR in [at-loader] ./src/pages/location/location.page.spec.ts:24:40 
TS2339: Property 'and' does not exist on type '(options?: GeolocationOptions) => Promise<Geoposition>'.

问题是:我怎样才能克服这个问题,让 TSLint 不再提示这段代码?

最佳答案

实际上正确的类型是:

  let mockGeolocation: jasmine.SpyObj<Geolocation>;
let mockEvents: jasmine.SpyObj<Events>;

关于typescript - Ionic + Jasmine + Tslint - 类型上不存在属性 'and',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43280693/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com