0

I am subscribing to route on ngOnInit.

constructor(private route: ActivatedRoute)

ngOnInit() {
  this.route.queryParamMap.subscribe(params => {
     this.mode = 'test';
  })
}

I was able to test all other methods in my component except the route which I am not sure how to test. I also dont see any exact answer for such question.

Can anyone please help writing unit test for this piece.

kaounKaoun
  • 601
  • 1
  • 9
  • 21

1 Answers1

0

You have to inject 'Router' and 'ActivatedRoute' in yours providers list of Test Bed. Something like below.

class RouterStub {
  url = '...';
  navigate(urlName: string) {
    return urlName;
  }
}

class MockActivatedRoute {
  queryParams = Observable.from([{}]);
} 

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      providers: [...
        { provide: Router, useClass: RouterStub },
        { provide: ActivatedRoute, useClass: MockActivatedRoute } ..
      ]
    })
ammadkh
  • 569
  • 3
  • 9