lathonez / clicker

Ionic 2 + @angular/cli Seed Project : Angular2 + Typescript + Karma + Protractor + Travis
http://lathonez.com/2018/ionic-2-unit-testing/
MIT License
430 stars 137 forks source link

Cannot read property '_getPortal' of undefined when testing a function that uses ToastController .present() #206

Closed kamok closed 7 years ago

kamok commented 7 years ago

When you have a toast that you present(), it throws this _getPortal error. Here's a basic function that you can invoke in a spec.

private presentToast(message: string): void {
    const toast = this.toastCtrl.create({
      message: message
    });
    toast.present();
  }

There's a stackoverflow post on it, using the technique of mocking the View Controller, which according to the OP, works.

But it hasn't worked for me using the clicker template, even though it uses the same mock. Any thoughts? Can anyone else replicate?

Here's the full error:


 TypeError: Cannot read property '_getPortal' of undefined
          at App.present (webpack:///Users/kamok/code/election/election-mobile/~/ionic-angular/components/app/app.js:78:0 <- src/test.ts:2354:35)
          at Toast.present (webpack:///Users/kamok/code/election/election-mobile/~/ionic-angular/components/toast/toast.js:36:0 <- src/test.ts:62146:26)
          at CheckInPage.presentToast (webpack:///Users/kamok/code/election/election-mobile/src/pages/check-in/check-in.ts:9:7402 <- src/test.ts:47767:7371)
          at CheckInPage.broadcastMember (webpack:///Users/kamok/code/election/election-mobile/src/pages/check-in/check-in.ts:9:7095 <- src/test.ts:47767:7063)
          at CheckInPage.onMemberSelect (webpack:///Users/kamok/code/election/election-mobile/src/pages/check-in/check-in.ts:9:4181 <- src/test.ts:47767:4149)
          at Object.<anonymous> (webpack:///Users/kamok/code/election/election-mobile/src/pages/check-in/check-in.spec.ts:44:15 <- src/test.ts:77420:22)
          at ZoneDelegate.invoke (webpack:///Users/kamok/code/election/election-mobile/~/zone.js/dist/zone.js:232:0 <- src/test.ts:112459:26)
          at ProxyZoneSpec.onInvoke (webpack:///Users/kamok/code/election/election-mobile/~/zone.js/dist/proxy.js:79:0 <- src/test.ts:48591:39)
          at ZoneDelegate.invoke (webpack:///Users/kamok/code/election/election-mobile/~/zone.js/dist/zone.js:231:0 <- src/test.ts:112458:32)
          at Zone.run (webpack:///Users/kamok/code/election/election-mobile/~/zone.js/dist/zone.js:114:0 <- src/test.ts:112341:43)
lathonez commented 7 years ago

As discussed previously, please use stack overflow.

If you want to draw our attention to the SO post, link it in #191.

Thanks