Ionic | "TypeError: this.platform.is is not a function when running unit tests

Ionic | "TypeError: this.platform.is is not a function when running unit tests

当我 运行 对我正在处理的 Ionic 项目进行单元测试时,我从 AppComponent 收到此错误,并且它发生在两个单元测试中:

App component should create the app

TypeError: this.platform.is is not a function

App component should initialize the app

TypeError: this.platform.is is not a function

这是我的 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 { 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 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 });
    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);
    expect(platformSpy.ready).toHaveBeenCalled();
    await platformReadySpy;
    expect(statusBarSpy.styleDefault).toHaveBeenCalled();
    expect(splashScreenSpy.hide).toHaveBeenCalled();
  });
});

这是据称导致错误的代码:

    if (this.platform.is('cordova') || this.platform.is('desktop') || this.platform.is('mobile')) {
        this.storageEngine = this.localStorage;
    } else {
        this.storageEngine = this.storage;
    }

当我查找此错误时,我能找到的唯一解决方案是为平台创建一个模拟对象,但我已经有了一个。任何人都知道我可以如何修复此错误?

你的错误正在给你答案。您已准备好模拟,但尚未模拟 'is' 函数。

试试这个。

platformSpy = jasmine.createSpyObj('Platform', { ready: platformReadySpy, 
is: (type: string) => { 
     if (type === 'cordova' || type === 'desktop' || type === 'mobile') {
       return true; 
     } else { 
       return false; 
     } 
   }
});