I am testing my Ionic 3 application with Jasmine, and I am wondering how to mock an AlertController
that creates a confirmation alert.
The function that creates the confirmation alert is the following:
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()
}
Basically, what I want is to create a mock for the AlertController
that simulates, for example, the user pressing the "yes" button so that I can test the code inside the Yes button handler. Following my unit test.
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');
});
I have looked to ionic3-mocks (link below), but I can't figure out how to force button actions inside an alert.
https://www.npmjs.com/package/ionic3-mocks
Hoang Dung Nguyen :
I'm working with Ionic 4 (and previously, 3) and adding new mocks seems to be troublesome to me as well. With AlertController, you can find something useful at my previous comment: https://stackoverflow.com/a/59193696/1594579.",
2019-12-26T09:41:48