Cannot read property '_getPortal' of undefined in ionic 2 Unit testing

泪湿孤枕 提交于 2019-11-29 14:53:38

Mock the LoadingController if the problem is from using LoadingController

export class LoadingControllerMock {
    _getPortal(): any { return {} };
    create(options?: any) { 
        return new LoadingMock()
    };
}

class LoadingMock {
    present() { };
    dismiss() { };
    dismissAll() { };
}

Import the Mock and the actual from wherever

import { LoadingController } from 'ionic-angular';

import { LoadingControllerMock } from '../../../../test-config/mocks-ionic';

Substitute

providers: [ 
    { provide: LoadingController, useClass: LoadingControllerMock }
]

I Solved this problem finally. I used a mock and defined required methods in that mock. Then It works :)
here is an example for a mock.

export class ViewControllerMock {
  public _setHeader(): any { return {} };
  public _setNavbar(): any { return {} };
  public _setIONContent(): any { return {} };
  public _setIONContentRef(): any { return {} };
}

then have to import that mock into your .spec.ts file as follows

import {ViewControllerMock} from '../../mocks';

then have to define that mock in your providers in spec.ts file as follows

providers: [{ provide: ViewController, useClass: ViewControllerMock}],
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!