如何在Ionic 3中模拟出ModalController

时间:2017-06-21 11:42:29

标签: angular unit-testing ionic-framework

模拟

export class ModalControllerMock {
    public create(param1,param2) {
        let rtn: Object = {};
        rtn['present'] = (() => true);
        return rtn;
    }; };

生产代码:

 someFunc() {
        let modal = this.modalController.create(myComp, {
            stuff:stuff
        });
        modal.present();
    }

错误

  

TypeError:undefined不是src / test.ts中的对象(评估'modal.present')(第61262行)

ModalController实施

https://github.com/ionic-team/ionic/blob/master/src/components/modal/modal-controller.ts

1 个答案:

答案 0 :(得分:0)

找到解决方案:

在mocks.ts中

import {ModalController} from "ionic-angular";
import { mockApp, mockConfig, mockDeepLinker } from 'ionic-angular/util/mock-providers';

export class ModalControllerMock {
    constructor() {
        return new ModalController(mockApp(), mockConfig(), mockDeepLinker());
    }
};

在spec文件中

spyOn(modalControllerMock, "create").and.callThrough();