我正在使用Jasmine测试我的Ionic3应用程序,我想知道如何模拟创建确认警报的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
创建一个模拟,例如,用户按下" Yes“按钮,这样我就可以在Yes按钮处理程序中测试代码。遵循我的单元测试。
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
发布于 2019-12-26 17:41:49
我正在使用Ionic 4(以及之前的3),添加新的mock对我来说似乎也很麻烦。使用AlertController
,你可以在我之前的评论中找到一些有用的东西:https://stackoverflow.com/a/59193696/1594579。
https://stackoverflow.com/questions/50573978
复制相似问题