Angular 6 – how to mock router.events url response in unit test

It can be as simple as:

TestBed.configureTestingModule({
  imports: [RouterTestingModule]
}).compileComponents()
    
...
    
const event = new NavigationEnd(42, "https://stackoverflow.com/", "https://stackoverflow.com/");
(TestBed.inject(Router).events as Subject<Event>).next(event);

Leave a Comment