Testing pages with Platform

So, i’m writing tests for my ionic app and one of my pages uses the Platform module to determine if the device accessing the app is a desktop or mobile. so Platform is one of the parameters in the constructor and I plug that in as a provider for my test but when I do I get the error TypeError: Cannot read property 'Ionic' of undefined any ideas?

I would mock Platform instead of trying to bring the real thing in.

1 Like

Okay so once I replaced the real platform with a mock i’m getting an error: “Error at injectionError at noProviderError…”

How are you setting up the testbed? I’ve done this successfully with something like the following:

TestBed.configureTestingModule({
  providers: [
    {provide: Platform, useClass: PlatformMock},
  ]
});

correct. that’s exactly what i’m doing and I used a mockplatform i found online: http://marclloyd.co.uk/angular2/mocking-ionic-platform-for-ionic-2-unit-tests/

I can’t read that site at all - it’s white-on-white text. Does it work with mine?

export class PlatformMock {
  registerBackButtonAction(fn: Function, priority?: number): Function {
    return new Function();
  }

  raf(callback: {
    (timeStamp?: number): void;
  } | Function): number {
    return 0;
  }

  hasFocus(elem: HTMLElement): boolean {
    return false;
  }
}

it just keeps complaining about the different functions that are missing. but I commented out platform within the page that my test is written for and i still got an injectionError providerError. so i’m not sure what the problem is.

By any chance, do you have providers[] included in any of the @Component decorations on any of the pages in your app?

no, the pages aren’t setup as i guess “modules” they are just pages that are imported into app module and then the providers are setup there.

Oh well. It was a guess based on this similar-sounding SO question.

that’s okay, thank you very much for your help though!