How to unit test a Angular 4 component which uses router.paramMap

后端 未结 5 874
星月不相逢
星月不相逢 2021-02-19 05:48

I m new to angular 4 and trying to test one of the angular 4 feature router.paramMap from unit tests, Reading route params in fallowing way and working as expected in my applica

5条回答
  •  死守一世寂寞
    2021-02-19 06:07

    You need to provide paramMap instead of params. paramMap should be of type ParamMap from @angular/router, so normal object can be converted to ParamMap using the method convertToParamMap() from @angular/routing.

    You can provide the mock ActivatedRoute like below:

    import { convertToParamMap} from '@angular/router';
    ....
    .....
    
    {
          provide: ActivatedRoute,
          useValue: { paramMap: Observable.of(convertToParamMap({id: 1})) }
    }
    

提交回复
热议问题