![](/img/trans.png)
[英]ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'map' of undefined
[英]Karma | Ionic | Uncaught Error: Uncaught Error: Uncaught (in promise): TypeError: Cannot read property 'getToken' of undefined
在我的 Ionic 应用程序上运行单元测试时,在它测试应用程序是否初始化的 app.component.spec.ts 中,我收到以下错误:
AppComponent should initialize the app
Uncaught Error: Uncaught (in promise): TypeError: Cannot read property 'getToken' of undefined
TypeError: Cannot read property 'getToken' of undefined
Failed: Uncaught (in promise): TypeError: Cannot read property 'getToken' of undefined
TypeError: Cannot read property 'getToken' of undefined
这是我的单元测试代码(app.component.spec.ts)
import { HttpClientTestingModule} from '@angular/common/http/testing';
import { CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import { TestBed, async } from '@angular/core/testing';
import { Platform } from '@ionic/angular';
import { SplashScreen } from '@ionic-native/splash-screen/ngx';
import { StatusBar } from '@ionic-native/status-bar/ngx';
import { MagentoService } from './services/magento.service';
import { AppComponent } from './app.component';
import { NativeStorage } from '@ionic-native/native-storage/ngx';
import { Router } from '@angular/router';
import { NativeAudio } from '@ionic-native/native-audio/ngx';
import { Dialogs } from '@ionic-native/dialogs/ngx';
import { FCM } from '@ionic-native/fcm/ngx';
describe('AppComponent', () => {
//let component = AppComponent;
let statusBarSpy, splashScreenSpy, platformReadySpy, platformSpy;
beforeEach(async(() => {
statusBarSpy = jasmine.createSpyObj('StatusBar', ['styleDefault']);
splashScreenSpy = jasmine.createSpyObj('SplashScreen', ['hide']);
platformReadySpy = Promise.resolve();
platformSpy = jasmine.createSpyObj('Platform', { ready: platformReadySpy,
is: (type: string) => {( type === 'cordova' || type === 'desktop' || type === 'mobile' )},
});
TestBed.configureTestingModule({
imports: [ HttpClientTestingModule ],
declarations: [AppComponent],
schemas: [CUSTOM_ELEMENTS_SCHEMA],
providers: [
{ provide: StatusBar, useValue: statusBarSpy },
{ provide: SplashScreen, useValue: splashScreenSpy },
{ provide: Platform , useValue: platformSpy },
{ provide: NativeStorage },
{ provide: Router },
{ provide: NativeAudio },
{ provide: Dialogs },
{ provide: FCM }
],
}).compileComponents();
}));
it('should create the app', () => {
const fixture = TestBed.createComponent(AppComponent);
const app = fixture.debugElement.componentInstance;
expect(app).toBeTruthy();
});
it('should initialize the app', async () => {
TestBed.createComponent(AppComponent);
platformSpy.ready();
statusBarSpy.styleDefault();
splashScreenSpy.hide();
expect(platformSpy.ready).toHaveBeenCalled();
await platformReadySpy;
expect(statusBarSpy.styleDefault).toHaveBeenCalled();
expect(splashScreenSpy.hide).toHaveBeenCalled();
});
});
这是错误中引用的代码(app.component.ts)
initializeApp() {
this.platform.ready().then(() => {
this.storageEngine.setItem('Platform', 'Android');
this.statusBar.styleDefault();
this.magentoService.getToken();
this.fcm.getToken().then(token => {
this.storageEngine.setItem('FCMToken', token);
});
//...snip
}
具体来说,错误消息中引用的是this.fcm.getToken().then
。 但是,我不完全确定如何处理这些信息。 我只是想测试应用程序是否正在初始化,我并不是想专门进行测试来测试这个 promise。 如果有人知道如何解决此错误,那将非常有帮助。
您必须模拟fcm
以获得getToken
function 并使其返回 promise。
将您的 providers 数组更改为:
providers: [
{ provide: StatusBar, useValue: statusBarSpy },
{ provide: SplashScreen, useValue: splashScreenSpy },
{ provide: Platform , useValue: platformSpy },
{ provide: NativeStorage }, // these provides without useValue or useFactory seem weird to me
{ provide: Router },
{ provide: NativeAudio },
{ provide: Dialogs },
{ provide: FCM, useValue: { getToken: () => Promise.resolve(true) } } // mock FCM like so
],
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.