离子| “ TypeError:运行单元测试时,this.platform.is不是函数

乔恩·提尔

当我为一个正在工作的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;
    }

当我查找此错误时,我唯一能找到的解决方案是为Platform创建一个模拟对象,但是我已经有了一个。有人知道我如何能够解决此错误吗?

麦吉尼

您的错误为您提供了答案。您已经准备好嘲笑,但是还没有嘲笑'is'函数。

试试这个。

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

错误TypeError:BlinkId不是构造函数离子4

离子控制器和服务:“ TypeError:...不是函数”

离子错误:TypeError:_co.toggleRecoder 不是函数

离子/角度8错误TypeError:this.sendData不是函数

离子-运行时错误this.fba.logEvent(...)。then不是函数

TypeError:_reactNativePushNotification.default.localNotificationSchedule在编写单元测试时不是函数

Nestjs mongoose 单元测试:TypeError: <functionName> 不是函数

角单元测试:使用provide useValue时“不是函数”

编写单元测试时,createUser 不是函数错误?

离子v4未被捕获的TypeError:Object(...)不是函数

离子性本地通知错误:未捕获(承诺):TypeError:Object(...)不是函数

离子-生成页面失败,并出现TypeError:AppScripts.processPageRequest不是函数

离子语音识别-运行时错误Object(...)不是hasPermission上的函数

TypeError:运行npm测试时environment.dispose不是函数

TypeError:Object(...)不是eval(platform.es5.js:81)的eval(platform.es5.js:79)的函数

TypeError:单元测试自定义redux钩子时undefined不是函数

角度4单元测试错误`TypeError:ctor不是构造函数`

AngularJS:单元测试控制器返回“ TypeError:$ scope。$ watch不是函数”

ReactJS单元测试-TypeError:this.props.onChange不是一个函数

角单元测试用例中的错误:TypeError:this.userService.currentUser.subscribe不是函数

Xcode:运行XC单元测试时,模块名称“”不是有效的标识符

单元测试网络响应。在调试时起作用,而不是在实际运行时起作用

IONIC Serve:运行时错误platform.toLowerCase不是函数

在离子单元测试中找不到Homepage的组件工厂

离子服务的单元测试未定义

单元测试角度/离子项目

使用 firebase-server 进行单元测试时,this.app.database 不是函数

尝试使用Jasmine和AngularJS进行单元测试时,参数'fn'不是函数

离子3:类型'any'不是构造函数类型