0
大家都不存在。我正在通過這個boilerplate構建一個Ionic應用程序。 現在,我正在爲特定頁面構建測試,並且我正在使用茉莉花來嘲笑提供程序並設置方法的預期行爲。 這就是beforeEach()如何方法看起來像看起來像:Ionic + Jasmine + Tslint - 屬性'和'
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,
],
},
); });
當我定義爲getCurrentPosition方法的承諾回報的問題開始:
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不再抱怨這個代碼?
看來你的問題不在於tslint。你的代碼'mockGeolocation.getCurrentPosition.and.callFake'是錯誤的。我認爲它需要像這樣:'mockGeolocation.create.and.getCurrentPosition ....'。看看其他模擬模式(例如'mockModalController') – Diullei