业力 |离子 |未捕获的错误:未捕获的错误:未捕获(承诺):类型错误:无法读取未定义的属性“getToken”

时间:2021-02-11 23:55:26

标签: angular typescript ionic-framework karma-jasmine

在我的 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

Here is the full error message

这是我的单元测试代码(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。但是,我不完全确定如何处理这些信息。我只是想测试应用程序是否正在初始化,我不想专门进行测试来测试这个承诺。如果有人知道如何解决此错误,那将非常有帮助。

1 个答案:

答案 0 :(得分:1)

您必须模拟 SELECT * INTO NewTable FROM OldTable TRUNCATE TABLE NewTable 才能拥有 fcm 函数并使其返回承诺。

将您的提供者数组更改为:

getToken