It seems there’s no way to tell Ionic to use a mock service to create a root component. There’s no way to provide the information regarding the mock service throughIonicModule.forRoot(root, config, deeplink)
.
My test code looks like this:
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [MyApp, LocationPage],
providers: [
{provide: RealService, useClass: MockService}
],
imports: [IonicModule.forRoot(MyApp)]
}).compileComponents().then(() => {
fixture = TestBed.createComponent(MyApp);
comp = fixture.componentInstance;
});
}));
The provider information is hard-coded in IonicModule.forRoot (the following code was taken from Ionic 2 src code):
IonicModule.forRoot = function (appRoot, config, deepLinkConfig) {
if (config === void 0) { config = null; }
if (deepLinkConfig === void 0) { deepLinkConfig = null; }
return {
ngModule: IonicModule,
providers: [
// useValue: bootstrap values
{ provide: AppRootToken, useValue: appRoot },
{ provide: ConfigToken, useValue: config },
{ provide: DeepLinkConfigToken, useValue: deepLinkConfig },
// useFactory: user values
{ provide: PlatformConfigToken, useFactory: providePlatformConfigs },
// useFactory: ionic core providers
{ provide: Platform, useFactory: setupPlatform, deps: [DOCUMENT, PlatformConfigToken, NgZone] },
{ provide: Config, useFactory: setupConfig, deps: [ConfigToken, Platform] },
// useFactory: ionic app initializers
{ provide: APP_INITIALIZER, useFactory: registerModeConfigs, deps: [Config], multi: true },
{ provide: APP_INITIALIZER, useFactory: registerTransitions, deps: [Config], multi: true },
{ provide: APP_INITIALIZER, useFactory: setupProvideEvents, deps: [Platform, DomController], multi: true },
{ provide: APP_INITIALIZER, useFactory: setupTapClick, deps: [Config, Platform, DomController, App, NgZone, GestureController], multi: true },
// useClass
{ provide: HAMMER_GESTURE_CONFIG, useClass: IonicGestureConfig },
// useValue
{ provide: ANALYZE_FOR_ENTRY_COMPONENTS, useValue: appRoot, multi: true },
// ionic providers
ActionSheetController,
AlertController,
App,
DomController,
Events,
Form,
GestureController,
Haptic,
Keyboard,
LoadingController,
Location,
MenuController,
ModalController,
PickerController,
PopoverController,
TapClick,
ToastController,
TransitionController,
{ provide: LocationStrategy, useFactory: provideLocationStrategy, deps: [PlatformLocation, [new Inject(APP_BASE_HREF), new Optional()], Config] },
{ provide: UrlSerializer, useFactory: setupUrlSerializer, deps: [DeepLinkConfigToken] },
{ provide: DeepLinker, useFactory: setupDeepLinker, deps: [App, UrlSerializer, Location] },
]
};
};