DEV Community

Discussion on: Testing Angular route guards with the RouterTestingModule

Collapse
 
layzee profile image
Lars Gyrup Brink Nielsen

When we use the TestBed we need to emulate parts of the Angular framework such as trigger change detection. The TestBed doesn't bootstrap an Angular application. No longer are all events caught by the NgZone triggering change detection. This is also the case here. If we don't wrap Router#navigate* in NgZone#run in tests, a warning is triggered.

Collapse
 
omeryousaf profile image
Omer Yousaf

Lars, in your tests you just check whether the navigation went through or was denied, right ?

expect(canNavigate).toBeFalse();
Enter fullscreen mode Exit fullscreen mode

So, was triggering change detection still necessary ?

Thread Thread
 
layzee profile image
Lars Gyrup Brink Nielsen

If we want to avoid warnings, yes. The warning will say that a scheduled event happened outside of the NgZone.

Thread Thread
 
layzee profile image
Lars Gyrup Brink Nielsen

We also want to make sure that changes are stable as they would be in an app.