Ionic + Jasmine + Tslint - 属性 'and' 类型不存在
Ionic + Jasmine + Tslint - Property 'and' does not exist on type
各位。我正在基于此 boilerplate 构建一个 Ionic 应用程序。
现在,我正在为一个特定的页面构建测试,我正在使用 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,
],
},
); } );
当我为 getCurrentPosition 方法定义承诺 return 时,问题就开始了:
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 不再抱怨这段代码?
问题解决了!
我忘了详细说明我声明变量的方式:
describe('Location Page', () => {
let mockLoadingController: any;
let mockModalController: any;
let mockGeolocation: Geolocation;
let mockEvents: Events;
beforeEach(() => {
//rest of the code here
如您所见,我为 mockGeolocation 变量定义了一个类型,并将其他类型设置为 any。将所有变量设置为 any 与 jasmine 完美配合,并且不会在 tslint 中产生错误。
所以正确的代码是这样的:
describe('Location Page', () => {
let mockLoadingController: any;
let mockModalController: any;
let mockGeolocation: any;
let mockEvents: any;
beforeEach(() => {
实际上正确的类型是:
let mockGeolocation: jasmine.SpyObj<Geolocation>;
let mockEvents: jasmine.SpyObj<Events>;
各位。我正在基于此 boilerplate 构建一个 Ionic 应用程序。 现在,我正在为一个特定的页面构建测试,我正在使用 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,
],
},
); } );
当我为 getCurrentPosition 方法定义承诺 return 时,问题就开始了:
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 不再抱怨这段代码?
问题解决了! 我忘了详细说明我声明变量的方式:
describe('Location Page', () => {
let mockLoadingController: any;
let mockModalController: any;
let mockGeolocation: Geolocation;
let mockEvents: Events;
beforeEach(() => {
//rest of the code here
如您所见,我为 mockGeolocation 变量定义了一个类型,并将其他类型设置为 any。将所有变量设置为 any 与 jasmine 完美配合,并且不会在 tslint 中产生错误。
所以正确的代码是这样的:
describe('Location Page', () => {
let mockLoadingController: any;
let mockModalController: any;
let mockGeolocation: any;
let mockEvents: any;
beforeEach(() => {
实际上正确的类型是:
let mockGeolocation: jasmine.SpyObj<Geolocation>;
let mockEvents: jasmine.SpyObj<Events>;