7

I've read the article about testing Ionic2 projects with TestBed and I've got the trouble when I'm trying to repeat example from article in my environment. When I try to start tests on Step 3 I have the error "No provider for StatusBar".

Probably it's a stupid question, but can somebody suppose why it happens?

StatusBar is included (imported) in my app.component.ts file.

import { StatusBar } from '@ionic-native/status-bar';
NobbyNobbs
  • 1,341
  • 1
  • 11
  • 17

2 Answers2

11

When using the TestBed, you are creating an entirely new @NgModule solely for testing. As such, you have to configure that test module to import all its dependencies - including, in your case, the status bar.

So, the same way you import the Status Bar in your main application (either directly or by importing its containing NgModule), you have to do the same for your test module.

TestBed.configureTestingModule({
  ......,
  providers: [
    StatusBar
  ]
})

or

 TestBed.configureTestingModule({
   imports: [
      ModuleContainingStatusBar
   ]     
})

(sorry, not familiar with Ionic itself so don't know how you import StatusBar for use)

snorkpete
  • 14,278
  • 3
  • 40
  • 57
  • Thank you! It works. But works a bit is strange. I should import StatusBar in test module again though it's should be already imported in tested component. – NobbyNobbs Mar 24 '17 at 09:09
  • it depends on what you mean by 'imported in tested component'. If you are referring to the ES6 import i.e. the import {bla} from 'blah' statement, then yes, that will appear all over the place. – snorkpete Mar 24 '17 at 18:21
  • Yes, _import {bla} from 'blah'_ it's exactly what I meant. – NobbyNobbs Mar 24 '17 at 19:37
2

Try by adding it to the providers array of your @NgModule

sebaferreras
  • 44,206
  • 11
  • 116
  • 134