Mocking router.events.subscribe() Angular2

前端 未结 6 1211
生来不讨喜
生来不讨喜 2020-12-30 00:03

In my app.component.ts I have the following ngOnInit function:

ngOnInit() {
    this.sub = this.router.events.subscribe(e => {
      if (e instanceof Navi         


        
6条回答
  •  无人及你
    2020-12-30 00:55

    This is a really old question but I just came across it looking for something better than what i have and in my case i need to test several different events. My basic approach was just to change the Router.events to a non read only value like

     (router as any).events = new BehaviorSubject(null);
     fixture.detectChanges();
     router.events.next(new NavigationEnd(0, 'http://localhost:4200/login', 
     'http://localhost:4200/login'));
      expect(comp.loggedIn).toEqual(true);
    

    Hope maybe that helps someone. I couldn't find an easier solution after looking around

提交回复
热议问题