Angular: Unit Testing Routing : Expected '' to be '/route' Angular: Unit Testing Routing : Expected '' to be '/route' typescript typescript

Angular: Unit Testing Routing : Expected '' to be '/route'


You forgot to import routes to the RouterTestingModule, in your test file.

You have to add export keyword to your const routes in your AppRoutingModule file, then you can import the routes in your test file ( and add them in your test configuration).

import {routes} from '...'; // I don't have the app-routing.module file path.......... beforeEach(() => {    TestBed.configureTestingModule({      imports: [RouterTestingModule.withRoutes(routes), <-- I added the routes here.                FormsModule , DxTemplateModule , HttpModule   ],      providers:    [SessionService , HttpService ],      declarations: [        AppComponent,        LoginComponent,        WelcomeComponent,        ApplicationParametersComponent,        InscriptionComponent      ],      schemas: [ CUSTOM_ELEMENTS_SCHEMA ]    });    router = TestBed.get(Router);    location = TestBed.get(Location);    fixture = TestBed.createComponent(AppComponent);    router.initialNavigation();  });

If you don't load routes in the router testing modules, it won't be able to know where to go when you navigate, so it will get back to original page with an error in console.

The tutorial you followed has a very strange way to handle routing because tick() is used for fakeAsync tests and this is a real async one. So you have to use the Promise<boolean> returned by router.navigate:

it('navigate to "inscription" takes you to /inscription', () => {    router.navigate(['inscription']).then(() => {        expect(location.path()).toBe('/inscription');    });});

As you see you can also remove fakeAsync because this is not fake, it's an async call.

See it on plunkr