angularjs - Mocking Components with Angular 2 -
i trying mock heroes-component of 'tour of heroes' angular 2 tutorial. don't know how mock router, needed instantiating heroescomponent. tried create spy jasmine, doesn't work, because missing property 'rootcomponenttype'.
how can make work?
import {heroescomponent} "./heroes.component"; import {router} "@angular/router-deprecated"; import {heroservice} "./hero.service"; describe('heroescomponent', () => { let heroes:heroescomponent; let router:router; let service:heroservice; beforeeach(() => { router = jasmine.createspyobj('router', ['navigate']); service = new heroservice(/* care later */); heroes = new heroescomponent(router, service); }); it('should defined', () => { expect(heroes).tobedefined(); }); });
it working now:
describe('heroescomponent', () => { let router:any; let heroescomponent:heroescomponent; let service:heroservice; beforeeach(() => { router = new class { navigate = jasmine.createspy("navigate"); }; heroescomponent = new heroescomponent(router, service); }); it('should defined', () => { expect(heroescomponent).tobedefined(); }); });
you create spy directly on navigate method.
using addproviders di:
beforeeach(() => addproviders([ { provide: router, useclass: class { navigate = jasmine.createspy("navigate"); } }]));
for directly initialization of component:
let router; let heroescomponent; beforeeach(() => { router = new class { navigate = jasmine.createspy("navigate"); }; heroescomponent = new heroescomponent(router); });
Comments
Post a Comment