Testing pages with Platform


#1

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?


#2

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


#3

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


#4

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

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

#5

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/


#6

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;
  }
}

#7

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.


#8

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


#9

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.


#10

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


#11

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