Search code examples
angularjasmine

How to transition away from deprecated TestBed.get()


Using Angular 14.

We have a couple of test suites, which need to dynamically change the parameters on ActivatedRoute, so they do

TestBed.get(ActivatedRoute).paramMap = of(convertToParamMap({
  < new parameters here >
}));

which is working fine, however the get has a strike-through marking indicating that it is deprecated from Angular 9 onwards, with a hint that you should use inject instead.

But whereas TestBed.inject(ActivatedRoute).paramMap is valid code, the returned paramMap reference becomes read-only, and so the assignment is not.

What's the correct approach here?


Solution

  • It's javascript, don't sweat it.

    
    Object.assign(TestBed.inject(ActivatedRoute), {
      paramMap: of(convertToParamMap({}))
    });