I'm learning Angular and I want to do tests, but I'm stuck. I've got a function:
ngOnInit(): void {
this.route.paramMap
.switchMap((params: ParamMap) =>
this.SomethingService.getSomething(params.get('id')))
.subscribe(something => {
this.something = something;
this.doSomethingElse();
});
}
where
route: ActivatedRoute
and I want to test it but I don't know how to mock ActivatedRoute
A simple way to mock
ActivatedRouteis this one:Then in your test it will be available and your function should work with this (at least the ActivatedRoute part)
You can get it with
TestBed.get(ActivatedRoute)in youritfunctions if you want to stock it in a variable.Don't forget to import Observable from
rxjs/Rxand not fromrxjs/Observable