2017-06-15 4 views
0

Je suis en train de tester un composant qui injectent son composant parent comme:angulaire 4 + Karma tester un composant

constructor(@Host() app: ParentComponent) { 
 
    ... 
 
}

Quand je lance le test, je reçois le message suivant:

erreur: aucun fournisseur pour ParentComponent

J'ai déjà i exclu toutes les dépendances utilisées dans les composants parent et enfant. Ai-je besoin de faire quelque chose de spécifique pour injecter le composant hôte?

Voici mon test complet:

describe('ChildComponent: Component',() => { 
 
\t //declare the component 
 
\t let comp: ChildComponent; 
 
\t let ca: ComponentFixture<ChildComponent>; 
 

 
\t //setup 
 
\t //first the async call to compile all the external templates 
 
\t beforeEach(async(() => { 
 
\t \t TestBed.configureTestingModule({ 
 
\t \t \t imports: [ 
 
\t \t \t \t ... 
 
\t \t \t ], 
 
\t \t \t declarations: [ 
 
\t \t \t \t ParentComponent, 
 
\t \t \t \t ChildComponent 
 
\t \t \t ], 
 
\t \t \t providers: [ 
 
\t \t \t \t ... 
 
\t \t \t ] 
 
\t \t }) 
 
\t \t .compileComponents(); 
 
\t })); 
 
\t //then the sync call to create the instance 
 
\t beforeEach(() => { 
 
\t \t 
 
\t \t ca = TestBed.createComponent(ChildComponent); 
 
\t \t comp = ca.componentInstance; 
 

 
\t \t ca.detectChanges(); 
 
\t }); 
 

 
\t afterEach(() => { 
 
\t \t comp = null; 
 
\t \t ca = null; 
 
\t }); 
 

 
\t //define the tests 
 
\t it('should have items...',() => { 
 
\t \t let count: number = comp.someArray.length; 
 
\t \t expect(count).toBeGreaterThan(0); \t \t 
 
\t }); 
 
});

Répondre

0

Vous devez spécifier un fournisseur pour ce composant.

TestBed.configureTestingModule({ 
 
\t \t \t imports: [ 
 
\t \t \t \t ... 
 
\t \t \t ], 
 
\t \t \t declarations: [ 
 
\t \t \t \t ParentComponent, 
 
\t \t \t \t ChildComponent 
 
\t \t \t ], 
 
\t \t \t providers: [ 
 
\t \t \t \t {provide: ParentComponent, useClass: ParentComponent} 
 
\t \t \t ] 
 
\t \t }) 
 
\t \t .compileComponents();