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.

  1. using addproviders di:

    beforeeach(() => addproviders([ {      provide: router,      useclass: class { navigate = jasmine.createspy("navigate"); } }])); 
  2. for directly initialization of component:

    let router; let heroescomponent;  beforeeach(() => {     router = new class { navigate = jasmine.createspy("navigate"); };     heroescomponent = new heroescomponent(router); }); 

Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -