How to unit test a component that depends on parameters from ActivatedRoute?

前端 未结 8 1110
离开以前
离开以前 2020-12-04 09:37

I am unit testing a component that is used to edit an object. The object has an unique id that is used in order to grab the specific object from an array of obj

8条回答
  •  渐次进展
    2020-12-04 09:58

    The simplest way to do this is to just use the useValue attribute and provide an Observable of the value you want to mock.

    RxJS < 6

    import { Observable } from 'rxjs/Observable';
    import 'rxjs/add/observable/of';
    ...
    {
      provide: ActivatedRoute,
      useValue: {
        params: Observable.of({id: 123})
      }
    }
    

    RxJS >= 6

    import { of } from 'rxjs';
    ...
    {
      provide: ActivatedRoute,
      useValue: {
        params: of({id: 123})
      }
    }
    

提交回复
热议问题