【发布时间】:2018-05-28 22:15:40
【问题描述】:
我正在使用 Jasmine 测试我的 Ionic 3 应用程序,我想知道如何模拟创建确认警报的 AlertController。
创建确认警报的函数如下:
pressedButton:string="";
myAlert() {
let confirm = this.alerCtrl.create({
title: 'Title',
message: 'Some message here',
buttons: [
{
text: 'No',
handler: () => {
this.pressedButton = 'No';
}
},
{
text: 'Yes',
handler: () => {
this.pressedButton = 'Yes';
}
}]
});
confirm.present()
}
基本上,我想要为AlertController 创建一个模拟,例如,模拟用户按下“是”按钮,以便我可以测试“是”按钮处理程序中的代码。按照我的单元测试。
beforeEach(() => {
fixture = TestBed.createComponent(MyPage);
comp = fixture.componentInstance;
});
it('should set pressedButton to "Yes" when the user press the "Yes" button', () => {
comp.myAlert(); //I want a mock that simulates the Yes button being pressed
expect(comp.pressedButton).toEqual('Yes');
});
我查看了 ionic3-mocks(下面的链接),但我不知道如何在警报中强制按钮操作。 https://www.npmjs.com/package/ionic3-mocks
【问题讨论】:
标签: unit-testing ionic-framework jasmine ionic3