Trouble unit testing service with the Router injected in the constructor in Angular 2 -


i'm trying test service has application router injected in constructor. following julie ralph's suggestions presented in angularconnect conf 2015 (and repository: https://github.com/juliemr/ng2-test-seed), i'm using karma , jasmine.

it follows example service tested:

import { router } 'angular2/router';  export class exampleservice {   constructor(router : router) {     this._router = router;   }    //... } 

right now, i'm asserting truth. follows service test:

import { it, describe, expect, inject, beforeeachproviders, mockapplicationref } 'angular2/testing'; import { router_providers } 'angular2/router'; import { provide, applicationref } 'angular2/core';  import { exampleservice } 'example-service.js';  describe('exampleservice', () => {   beforeeachproviders(() => [     router_providers, exampleservice,     provide(applicationref, { useclass: mockapplicationref })   ]);    it('should validate truth', inject([exampleservice], (exservice) => {     expect(true).tobetruthy();   })); }); 

when run tests ( > karma start karma.config.js ), typeerror: cannot read property 'length' of null

looking @ router.js source code, looks should bootstrap @ least 1 component before injecting router. there easy way inject router dependency in test?

the stacktrace:

original exception: typeerror: cannot read property 'length' of null original stacktrace: typeerror: cannot read property 'length' of null @ routerprimarycomponentfactory (c:/users/lsantos/desktop/ng2-test-seed/node_modules/angular2/bundles/router.js:2963:27) @ injector._instantiate (c:/users/lsantos/desktop/ng2-test-seed/node_modules/angular2/bundles/angular2.js:11920:19) @ injector._instantiateprovider (c:/users/lsantos/desktop/ng2-test-seed/node_modules/angular2/bundles/angular2.js:11859:21) @ injector._new (c:/users/lsantos/desktop/ng2-test-seed/node_modules/angular2/bundles/angular2.js:11849:19) @ injectordynamicstrategy.getobjbykeyid (c:/users/lsantos/desktop/ng2-test-seed/node_modules/angular2/bundles/angular2.js:11733:42) @ injector._getbykeydefault (c:/users/lsantos/desktop/ng2-test-seed/node_modules/angular2/bundles/angular2.js:12048:33) @ injector._getbykey (c:/users/lsantos/desktop/ng2-test-seed/node_modules/angular2/bundles/angular2.js:12002:21) @ injector._getbydependency (c:/users/lsantos/desktop/ng2-test-seed/node_modules/angular2/bundles/angular2.js:11990:21) @ injector._instantiate (c:/users/lsantos/desktop/ng2-test-seed/node_modules/angular2/bundles/angular2.js:11887:32) @ injector._instantiateprovider (c:/users/lsantos/desktop/ng2-test-seed/node_modules/angular2/bundles/angular2.js:11859:21) @ injector._new (c:/users/lsantos/desktop/ng2-test-seed/node_modules/angular2/bundles/angular2.js:11849:19) @ injectordynamicstrategy.getobjbykeyid (c:/users/lsantos/desktop/ng2-test-seed/node_modules/angular2/bundles/angular2.js:11733:42) @ injector._getbykeydefault (c:/users/lsantos/desktop/ng2-test-seed/node_modules/angular2/bundles/angular2.js:12048:33) @ injector._getbykey (c:/users/lsantos/desktop/ng2-test-seed/node_modules/angular2/bundles/angular2.js:12002:21) @ injector._getbydependency (c:/users/lsantos/desktop/ng2-test-seed/node_modules/angular2/bundles/angular2.js:11990:21) @ injector._instantiate (c:/users/lsantos/desktop/ng2-test-seed/node_modules/angular2/bundles/angular2.js:11887:32) @ injector._instantiateprovider (c:/users/lsantos/desktop/ng2-test-seed/node_modules/angular2/bundles/angular2.js:11859:21) @ injector._new (c:/users/lsantos/desktop/ng2-test-seed/node_modules/angular2/bundles/angular2.js:11849:19) @ injectordynamicstrategy.getobjbykeyid (c:/users/lsantos/desktop/ng2-test-seed/node_modules/angular2/bundles/angular2.js:11733:42) @ injector._getbykeydefault (c:/users/lsantos/desktop/ng2-test-seed/node_modules/angular2/bundles/angular2.js:12048:33) @ injector._getbykey (c:/users/lsantos/desktop/ng2-test-seed/node_modules/angular2/bundles/angular2.js:12002:21) @ injector._getbydependency (c:/users/lsantos/desktop/ng2-test-seed/node_modules/angular2/bundles/angular2.js:11990:21) @ injector._instantiate (c:/users/lsantos/desktop/ng2-test-seed/node_modules/angular2/bundles/angular2.js:11887:32) @ injector._instantiateprovider (c:/users/lsantos/desktop/ng2-test-seed/node_modules/angular2/bundles/angular2.js:11859:21) @ injector._new (c:/users/lsantos/desktop/ng2-test-seed/node_modules/angular2/bundles/angular2.js:11849:19) @ injectordynamicstrategy.getobjbykeyid (c:/users/lsantos/desktop/ng2-test-seed/node_modules/angular2/bundles/angular2.js:11733:42) @ injector._getbykeydefault (c:/users/lsantos/desktop/ng2-test-seed/node_modules/angular2/bundles/angular2.js:12048:33) @ injector._getbykey (c:/users/lsantos/desktop/ng2-test-seed/node_modules/angular2/bundles/angular2.js:12002:21) @ injector.get (c:/users/lsantos/desktop/ng2-test-seed/node_modules/angular2/bundles/angular2.js:11804:19) @ c:/users/lsantos/desktop/ng2-test-seed/node_modules/angular2/bundles/testing.dev.js:2477:25 @ array.map (native) @ array.map (c:/users/lsantos/desktop/ng2-test-seed/node_modules/es6-shim/es6-shim.js:1113:14) @ functionwithparamtokens.execute (c:/users/lsantos/desktop/ng2-test-seed/node_modules/angular2/bundles/testing.dev.js:2476:33) @ c:/users/lsantos/desktop/ng2-test-seed/node_modules/angular2/bundles/testing.dev.js:2601:25 @ zone.run (c:/users/lsantos/desktop/ng2-test-seed/node_modules/angular2/bundles/angular2-polyfills.js:138:17) @ zone.run (c:/users/lsantos/desktop/ng2-test-seed/node_modules/angular2/bundles/testing.dev.js:2544:30) @ runintestzone (c:/users/lsantos/desktop/ng2-test-seed/node_modules/angular2/bundles/testing.dev.js:2588:23) @ object. (c:/users/lsantos/desktop/ng2-test-seed/node_modules/angular2/bundles/testing.dev.js:2600:33) @ attemptasync (c:/users/lsantos/desktop/ng2-test-seed/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:1819:24) @ queuerunner.run (c:/users/lsantos/desktop/ng2-test-seed/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:1774:9) @ queuerunner.execute (c:/users/lsantos/desktop/ng2-test-seed/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:1762:10) @ spec.env.queuerunnerfactory (c:/users/lsantos/desktop/ng2-test-seed/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:627:35) @ spec.execute (c:/users/lsantos/desktop/ng2-test-seed/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:353:10) @ object.fn (c:/users/lsantos/desktop/ng2-test-seed/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:2360:37) @ attemptasync (c:/users/lsantos/desktop/ng2-test-seed/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:1819:24) @ queuerunner.run (c:/users/lsantos/desktop/ng2-test-seed/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:1774:9) @ queuerunner.execute (c:/users/lsantos/desktop/ng2-test-seed/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:1762:10) @ env.queuerunnerfactory (c:/users/lsantos/desktop/ng2-test-seed/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:627:35) @ object.fn (c:/users/lsantos/desktop/ng2-test-seed/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:2345:13) @ attemptasync (c:/users/lsantos/desktop/ng2-test-seed/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:1819:24) @ queuerunner.run (c:/users/lsantos/desktop/ng2-test-seed/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:1774:9) @ c:/users/lsantos/desktop/ng2-test-seed/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:1801:16 @ c:/users/lsantos/desktop/ng2-test-seed/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:1745:9 @ queuerunnerfactory.oncomplete (c:/users/lsantos/desktop/ng2-test-seed/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:2348:17) @ queuerunner.clearstack (c:/users/lsantos/desktop/ng2-test-seed/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:605:9) @ queuerunner.run (c:/users/lsantos/desktop/ng2-test-seed/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:1784:12) @ c:/users/lsantos/desktop/ng2-test-seed/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:1801:16 @ c:/users/lsantos/desktop/ng2-test-seed/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:1745:9 @ complete (c:/users/lsantos/desktop/ng2-test-seed/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:365:9)

see https://github.com/angular/angular/issues/6325

problem solved, need provide router_primary_component.

import {router_primary_component} 'angular2/router';  class mockprimarycomponent { }  beforeeachproviders(() => [   router_providers,   provide(router_primary_component, {useclass: mockprimarycomponent}),   exampleservice,   provide(applicationref, { useclass: mockapplicationref }) ]); 

Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

c - Unrecognised emulation mode: elf_i386 on MinGW32 -