2016-02-01 23 views
7

sto scrivendo la componente più semplice che utilizza un routeLink:angolari 2: componenti di test con router

@Component({ 
    selector: 'memorySnippet', 
    templateUrl: '<div class="memory-snippet-wrapper" *ngIf="memory" 
        [routerLink]="['MainPanel', 'MemoryPanel', {'id' : this.memory.id}]">', 
    directives: [CORE_DIRECTIVES, ROUTER_DIRECTIVES] 
}) 
export class MemorySnippetComponent { 
    @Input() memory: Memory; 
} 

Il problema si verifica quando provo testare questa componente. Nel momento in cui aggiungo il link router Karma si lamenta provider mancanti:

Dopo aver aggiunto tutti i fornitori di Karma sta chiedendo ottengo questo:

beforeEachProviders(() => [ 
    MemorySnippetComponent, 
    MEMORY_SERVICE_PROVIDERS, 
    ROUTER_PROVIDERS, 
    ApplicationRef 
]); 

Ma quando ho eseguito il test ottengo questo errore:

EXCEPTION: EXCEPTION: Error during instantiation of Token RouterPrimaryComponent! (RouterLink -> Router -> RouteRegistry -> Token RouterPrimaryComponent).

ORIGINAL EXCEPTION: unimplemented

ORIGINAL STACKTRACE: Error: unimplemented

Cosa sto facendo male ??? Angular 2 (2.0.0-beta.1) non è ancora pronto per testare componenti con direttive router?

+0

Questa sarebbe la mia interpretazione di questo errore. È ancora in versione beta e le guide di testing non sono state rilasciate per questo tipo di test. Forse potresti trovare il RouterPrimaryComponent nel sorgente e implementare l'interfaccia, quindi usare 'provide (RouterPrimaryComponent, {useClass: MyRouterPrimaryComponent})' per sovrascriverlo e magari far funzionare i test. – SnareChops

+0

controlla il repository con il seme di prova dal team di Angular -> https://github.com/juliemr/ng2-test-seed relativo da questo talk https://www.youtube.com/watch?v=C0F2E-PRm44 –

+0

Questa sintassi non ha bisogno di un ritorno? 'beforeEachProviders (() => {return [ MemorySnippetComponent, MEMORY_SERVICE_PROVIDERS, ROUTER_PROVIDERS, ApplicationRef ]}); –

risposta

5

Per RC4 con nuovo router ora utilizzare ...

beforeEach(() => addProviders([ 
    APP_ROUTER_PROVIDERS, // must be first 
    {provide: APP_BASE_HREF, useValue: '/'}, // must be second 
    {provide: ActivatedRoute, useClass: Mock}, 
    {provide: Router, useClass: Mock} 
])); 

Un progetto github utilizzando questo approccio è ...

https://github.com/danday74/angular2-coverage

9

Si dovrebbe avere un metodo beforeEachProviders che sembra:

import {MockApplicationRef} from '@angular/core/testing'; 

beforeEachProviders(() => [ 
    ROUTER_PROVIDERS, 
    provide(APP_BASE_HREF, {useValue: '/'}), 
    provide(ROUTER_PRIMARY_COMPONENT, {useValue: YourComponent}), 
    provide(ApplicationRef, {useClass: MockApplicationRef} 
]); 

MockApplicationRef è fornito dal quadro di riferimento per questo tipo di test.

+0

Grazie @cexbrayat, ha funzionato! puoi per favore indicare la fonte di tale documentazione? –

+0

Questo è un test unitario dal mio ebook ^^ books.ninja-squad.com. Ma penso di averlo trovato nel codice sorgente del framework AFAIR. – cexbrayat

+0

Ottimo libro, grazie! –

Problemi correlati