I missed the <router-outlet> messages in other unit tests, but to have a good isolated example, I created AuthGuard that checks if a user is logged in for specific actions.
This is the code:
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) { if (!this.authService.isLoggedIn()) { this.router.navigate(['/login']); return false; } return true; }
Now I want to write unit test for this.
This is how I start the test:
beforeEach(() => { TestBed.configureTestingModule({ imports: [ RouterTestingModule.withRoutes([ { path: 'login', component: DummyComponent } ]) ], declarations: [ DummyComponent ], providers: [ AuthGuardService, { provide: AuthService, useClass: MockAuthService } ] }); });
I created a DummyComponent that does nothing. Now my test. Imagine that the service returns false and runs this.router.navigate(['/login']) :
it('should not let users pass when not logged in', (): void => { expect(authGuardService.canActivate(<any>{}, <any>{})).toBe(false); });
This will result in exclusion from the section βUnable to find bootable first outletβ. Obviously, I can use toThrow() instead of toBe(false) , but this does not seem like a very reasonable solution. Since I'm testing the service here, there is no template where I can put the <router-outlet> . I could mock the router and make my own navigation function, but then what is the point of the RouterTestingModule? You might even want to check how the navigation works.
source share