Unit testing - Getting issue with Alert Controller in Ionic Framework

十年热恋 提交于 2020-02-06 20:26:52

问题


I am trying to write a testcase for a method which has been called or not. Inside that method, I am calling an alert confirmation box.

I am getting an error like

Failed: this.alertCtrl.create is not a function

Component.ts
submitTicket(comments) {
if (comments.length > 0) {
  const prompt = this.alertCtrl.create({
    title: "<span>  Improve Solution  </span>",
    message: "<span>" + 'Are you sure you want  <br>' + "</span>" +
      "<span>" + 'to submit this improvement' + "</span>",
    enableBackdropDismiss: false,
    buttons: [
      {
        text: 'Cancel',
        handler: data => {
          // Some stuff
        }            
      },
      {
        text: 'Improve Solution',
        handler: data => {
         //Some stuff
        }
      }
    ]
  });
  prompt.present();
} else {
  this.errorMsg = true;
}
}
component.spec.ts
import {AlertControllerMock } from 'ionic-mocks';

beforeEach(async(()=> {
TestBed.configureTestingModule({
    declarations: [ImprovedsolutionsPage],
    imports: [
        IonicModule.forRoot(ImprovedsolutionsPage),
        HttpClientTestingModule
    ],
    providers: [
        NavController,
        AppService,
        AlertController,
        ImprovedsolutionsPage,
        {provide: ViewController, useClass: ViewControllerMock},
        {provide: LoadingController, useClass: LoadingControllerMock},
        {provide: AlertController, useClass: AlertControllerMock},          
    ]
}).compileComponents
}))
beforeEach(()=> {
    fixture=TestBed.createComponent(ImprovedsolutionsPage)
    component=fixture.componentInstance
   fixture.detectChanges()
})

it('should be call submitTicket method', async(()= > {
spyOn(component, 'submitTicket').and.callThrough()

let comment='Needs to improve in detailing '
component.submitTicket(comment)
expect(component.submitTicket).toHaveBeenCalled()

}))

Here I am using ionic-mocks module and I imported AlertControllerMock as shown in the above code. And I am using ionic version 3. For testing I am using Karma and jasmine

Could someone please help me out in this issue.


回答1:


[Updated] Here is my tip: I am not able to check your mock's implementation. However, make sure you have a separate boolean var to testify if the mocked function, for example, create, is called elsewhere.

create(opts?: AlertOptions): Promise<HTMLIonAlertElement> {
        this.createAlertCalled = true;
        this.opts = opts;

        const self = this;
        return Promise.resolve(<HTMLIonAlertElement>{
            present: (): Promise<void> => {
                self.presentCalled = true;
                return Promise.resolve();
            }
        });
    }

Subsequently, do a spyOn with AlertControllerMock on the mentioned variable and expect the assertion to be truthy.



来源:https://stackoverflow.com/questions/56446872/unit-testing-getting-issue-with-alert-controller-in-ionic-framework

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