2016-01-07 10 views
7

Próbuję przetestować usługę, która ma router aplikacji wstrzyknięty w jego konstruktorze. Po sugestiach Julie Ralph przedstawionych w AngularConnect conf 2015 (i jej repozytorium: https://github.com/juliemr/ng2-test-seed), używam Karmy i Jasmine.Usługa testowania jednostki dyskietki za pomocą routera wstrzykniętego w konstruktorze w Angular 2

Wynika usługę przykład być testowane:

import { Router } from 'angular2/router'; 

export class ExampleService { 
    constructor(router : Router) { 
    this._router = router; 
    } 

    //... 
} 

Teraz jestem po prostu stwierdzając prawdę. Wynika test serwisowy:

import { it, describe, expect, inject, beforeEachProviders, MockApplicationRef } from 'angular2/testing'; 
import { ROUTER_PROVIDERS } from 'angular2/router'; 
import { provide, ApplicationRef } from 'angular2/core'; 

import { ExampleService } from 'example-service.js'; 

describe('ExampleService',() => { 
    beforeEachProviders(() => [ 
    ROUTER_PROVIDERS, ExampleService, 
    provide(ApplicationRef, { useClass: MockApplicationRef }) 
    ]); 

    it('should validate the truth', inject([ExampleService], (exService) => { 
    expect(true).toBeTruthy(); 
    })); 
}); 

kiedy uruchomić testy (> karma rozpocznie karma.config.js), otrzymuję TypeError: nie można odczytać właściwość „długość” null

Patrząc na routerze .js kod źródłowy, wygląda na to, że powinienem załadować przynajmniej jeden komponent przed wstrzyknięciem routera. Czy istnieje łatwy sposób wstrzyknięcia zależności Routera w teście?

The StackTrace:

ORIGINAL EXCEPTION: TypeError: Cannot read property 'length' of null ORIGINAL STACKTRACE: TypeError: Cannot read property 'length' of null at routerPrimaryComponentFactory (C:/Users/LSANTOS/Desktop/ng2-test-seed/node_modules/angular2/bundles/router.js:2963:27) at Injector._instantiate (C:/Users/LSANTOS/Desktop/ng2-test-seed/node_modules/angular2/bundles/angular2.js:11920:19) at Injector._instantiateProvider (C:/Users/LSANTOS/Desktop/ng2-test-seed/node_modules/angular2/bundles/angular2.js:11859:21) at Injector._new (C:/Users/LSANTOS/Desktop/ng2-test-seed/node_modules/angular2/bundles/angular2.js:11849:19) at InjectorDynamicStrategy.getObjByKeyId (C:/Users/LSANTOS/Desktop/ng2-test-seed/node_modules/angular2/bundles/angular2.js:11733:42) at Injector._getByKeyDefault (C:/Users/LSANTOS/Desktop/ng2-test-seed/node_modules/angular2/bundles/angular2.js:12048:33) at Injector._getByKey (C:/Users/LSANTOS/Desktop/ng2-test-seed/node_modules/angular2/bundles/angular2.js:12002:21) at Injector._getByDependency (C:/Users/LSANTOS/Desktop/ng2-test-seed/node_modules/angular2/bundles/angular2.js:11990:21) at Injector._instantiate (C:/Users/LSANTOS/Desktop/ng2-test-seed/node_modules/angular2/bundles/angular2.js:11887:32) at Injector._instantiateProvider (C:/Users/LSANTOS/Desktop/ng2-test-seed/node_modules/angular2/bundles/angular2.js:11859:21) at Injector._new (C:/Users/LSANTOS/Desktop/ng2-test-seed/node_modules/angular2/bundles/angular2.js:11849:19) at InjectorDynamicStrategy.getObjByKeyId (C:/Users/LSANTOS/Desktop/ng2-test-seed/node_modules/angular2/bundles/angular2.js:11733:42) at Injector._getByKeyDefault (C:/Users/LSANTOS/Desktop/ng2-test-seed/node_modules/angular2/bundles/angular2.js:12048:33) at Injector._getByKey (C:/Users/LSANTOS/Desktop/ng2-test-seed/node_modules/angular2/bundles/angular2.js:12002:21) at Injector._getByDependency (C:/Users/LSANTOS/Desktop/ng2-test-seed/node_modules/angular2/bundles/angular2.js:11990:21) at Injector._instantiate (C:/Users/LSANTOS/Desktop/ng2-test-seed/node_modules/angular2/bundles/angular2.js:11887:32) at Injector._instantiateProvider (C:/Users/LSANTOS/Desktop/ng2-test-seed/node_modules/angular2/bundles/angular2.js:11859:21) at Injector._new (C:/Users/LSANTOS/Desktop/ng2-test-seed/node_modules/angular2/bundles/angular2.js:11849:19) at InjectorDynamicStrategy.getObjByKeyId (C:/Users/LSANTOS/Desktop/ng2-test-seed/node_modules/angular2/bundles/angular2.js:11733:42) at Injector._getByKeyDefault (C:/Users/LSANTOS/Desktop/ng2-test-seed/node_modules/angular2/bundles/angular2.js:12048:33) at Injector._getByKey (C:/Users/LSANTOS/Desktop/ng2-test-seed/node_modules/angular2/bundles/angular2.js:12002:21) at Injector._getByDependency (C:/Users/LSANTOS/Desktop/ng2-test-seed/node_modules/angular2/bundles/angular2.js:11990:21) at Injector._instantiate (C:/Users/LSANTOS/Desktop/ng2-test-seed/node_modules/angular2/bundles/angular2.js:11887:32) at Injector._instantiateProvider (C:/Users/LSANTOS/Desktop/ng2-test-seed/node_modules/angular2/bundles/angular2.js:11859:21) at Injector._new (C:/Users/LSANTOS/Desktop/ng2-test-seed/node_modules/angular2/bundles/angular2.js:11849:19) at InjectorDynamicStrategy.getObjByKeyId (C:/Users/LSANTOS/Desktop/ng2-test-seed/node_modules/angular2/bundles/angular2.js:11733:42) at Injector._getByKeyDefault (C:/Users/LSANTOS/Desktop/ng2-test-seed/node_modules/angular2/bundles/angular2.js:12048:33) at Injector._getByKey (C:/Users/LSANTOS/Desktop/ng2-test-seed/node_modules/angular2/bundles/angular2.js:12002:21) at Injector.get (C:/Users/LSANTOS/Desktop/ng2-test-seed/node_modules/angular2/bundles/angular2.js:11804:19) at C:/Users/LSANTOS/Desktop/ng2-test-seed/node_modules/angular2/bundles/testing.dev.js:2477:25 at Array.map (native) at Array.map (C:/Users/LSANTOS/Desktop/ng2-test-seed/node_modules/es6-shim/es6-shim.js:1113:14) at FunctionWithParamTokens.execute (C:/Users/LSANTOS/Desktop/ng2-test-seed/node_modules/angular2/bundles/testing.dev.js:2476:33) at C:/Users/LSANTOS/Desktop/ng2-test-seed/node_modules/angular2/bundles/testing.dev.js:2601:25 at Zone.run (C:/Users/LSANTOS/Desktop/ng2-test-seed/node_modules/angular2/bundles/angular2-polyfills.js:138:17) at Zone.run (C:/Users/LSANTOS/Desktop/ng2-test-seed/node_modules/angular2/bundles/testing.dev.js:2544:30) at runInTestZone (C:/Users/LSANTOS/Desktop/ng2-test-seed/node_modules/angular2/bundles/testing.dev.js:2588:23) at Object. (C:/Users/LSANTOS/Desktop/ng2-test-seed/node_modules/angular2/bundles/testing.dev.js:2600:33) at attemptAsync (C:/Users/LSANTOS/Desktop/ng2-test-seed/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:1819:24) at QueueRunner.run (C:/Users/LSANTOS/Desktop/ng2-test-seed/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:1774:9) at QueueRunner.execute (C:/Users/LSANTOS/Desktop/ng2-test-seed/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:1762:10) at Spec.Env.queueRunnerFactory (C:/Users/LSANTOS/Desktop/ng2-test-seed/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:627:35) at Spec.execute (C:/Users/LSANTOS/Desktop/ng2-test-seed/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:353:10) at Object.fn (C:/Users/LSANTOS/Desktop/ng2-test-seed/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:2360:37) at attemptAsync (C:/Users/LSANTOS/Desktop/ng2-test-seed/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:1819:24) at QueueRunner.run (C:/Users/LSANTOS/Desktop/ng2-test-seed/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:1774:9) at QueueRunner.execute (C:/Users/LSANTOS/Desktop/ng2-test-seed/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:1762:10) at Env.queueRunnerFactory (C:/Users/LSANTOS/Desktop/ng2-test-seed/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:627:35) at Object.fn (C:/Users/LSANTOS/Desktop/ng2-test-seed/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:2345:13) at attemptAsync (C:/Users/LSANTOS/Desktop/ng2-test-seed/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:1819:24) at QueueRunner.run (C:/Users/LSANTOS/Desktop/ng2-test-seed/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:1774:9) at C:/Users/LSANTOS/Desktop/ng2-test-seed/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:1801:16 at C:/Users/LSANTOS/Desktop/ng2-test-seed/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:1745:9 at queueRunnerFactory.onComplete (C:/Users/LSANTOS/Desktop/ng2-test-seed/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:2348:17) at QueueRunner.clearStack (C:/Users/LSANTOS/Desktop/ng2-test-seed/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:605:9) at QueueRunner.run (C:/Users/LSANTOS/Desktop/ng2-test-seed/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:1784:12) at C:/Users/LSANTOS/Desktop/ng2-test-seed/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:1801:16 at C:/Users/LSANTOS/Desktop/ng2-test-seed/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:1745:9 at complete (C:/Users/LSANTOS/Desktop/ng2-test-seed/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:365:9)

Zobacz także https://github.com/angular/angular/issues/6325

Odpowiedz

6

Problem rozwiązany, wystarczy podać ROUTER_PRIMARY_COMPONENT.

+1

Czy udało Ci się również skonfigurować router do testu za pomocą router.navigate()? –

+0

Jak to działa z najnowszym rc1? – hmrc87

3

Wiem, że ten wpis jest związany ze starym routerem, ale pomyślałem, że może to być przydatne, aby odpowiedzieć na to pytanie. Korzystanie kątową wersji rc.1 z nowym routerem, mam testów jednostkowych z routerem, w tym testowania navigateByUrl, za pomocą tego testu w ciągu kątowa jako inspiracja: https://github.com/angular/angular/blob/master/modules/%40angular/router/test/integration_spec.ts

zapisane mi wiele hastle

Oto robocza przykład

import {setBaseTestProviders,beforeEachProviders,inject,it,describe,expect,beforeEach} from '@angular/core/testing' 

import { Component,provide} from '@angular/core'; 
import {Routes,ROUTER_DIRECTIVES,Route} from "@angular/router"; 

import {ComponentResolver} from '@angular/core'; 
import {Router,RouterOutletMap,RouteSegment,RouterUrlSerializer,DefaultRouterUrlSerializer} from '@angular/router'; 
import {SpyLocation} from '@angular/common/testing'; 
import {Location} from '@angular/common'; 
import {ComponentFixture, TestComponentBuilder} from '@angular/compiler/testing'; 

import {TEST_BROWSER_DYNAMIC_PLATFORM_PROVIDERS,TEST_BROWSER_DYNAMIC_APPLICATION_PROVIDERS} from '@angular/platform-browser-dynamic/testing'; 



@Component({ 
    selector: 'some-component', 
    template: `Blah!`, 
    directives: [ROUTER_DIRECTIVES] 
}) 
export class SomeComponent { 
} 

@Component({ 
    selector: 'another-component', 
    template: `Blah!`, 
    directives: [ROUTER_DIRECTIVES] 
}) 
export class AnotherComponent { 
} 

@Component({ 
    selector: 'root-cmp', 
    template: `<router-outlet></router-outlet>`, 
    directives: [ROUTER_DIRECTIVES] 
}) 
@Routes([ 
    new Route({path: '/some-path',component:SomeComponent}), 
    new Route({path: '/another-path',component:AnotherComponent}) 
    ]) 
export class RootCmp { 
} 

export const PROVIDERS_TESTING = [ 
    provide(RouterUrlSerializer, {useClass: DefaultRouterUrlSerializer}), 
    RouterOutletMap, 
    provide(Location, {useClass: SpyLocation}), 
    provide(RouteSegment, {useFactory: (r) => r.routeTree.root, deps: [Router]}), 
    provide(Router,{ 
     useFactory: (resolver, urlParser, outletMap, location) => new Router(
      "RootComponent", RootCmp, resolver, urlParser, outletMap, location), 
      deps: [ComponentResolver, RouterUrlSerializer, RouterOutletMap, Location] 
     } 
    ),  
] 

setBaseTestProviders(TEST_BROWSER_DYNAMIC_PLATFORM_PROVIDERS,[TEST_BROWSER_DYNAMIC_APPLICATION_PROVIDERS,PROVIDERS_TESTING]); 

it('some test',inject([Router, TestComponentBuilder, Location], (router:Router, tcb:TestComponentBuilder, location:Location) => { 
    return new Promise((resolve,reject)=>{ 
     tcb.createAsync(RootCmp).then(fixture=>{         
      router.navigateByUrl('/some-path').then(()=>{ 
       expect(location.path()).toEqual('/some-path'); 
       resolve() 
      }) 
     }) 
    }) 
})) 

it('another test',inject([Router, TestComponentBuilder, Location], (router:Router, tcb:TestComponentBuilder, location:Location) => { 
    return new Promise((resolve,reject)=>{ 
     tcb.createAsync(RootCmp).then(fixture=>{         
      router.navigateByUrl('/another-path').then(()=>{ 
       expect(location.path()).toEqual('/another-path'); 
       resolve() 
      }) 
     }) 
    }) 
})) 
Powiązane problemy