I have tried it, and it works fine with your code.
There's no problem with equality in the method toContain
.
Have you changed const routes
to export const routes
in your app-routing.module.ts
, and import { routes } from './app-routing.module'
in your test?
It's the only thing that I had to change in order to test the app.
I have done the following steps:
ng new test-routing
(saying yes to routing so the app-routing.module.ts
is created.
- Modified
app.component.spec.ts
with the following code:
import { TestBed, async } from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing';
import { AppComponent } from './app.component';
import { routes } from './app-routing.module';
describe('AppComponent', () => {
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
RouterTestingModule
],
declarations: [
AppComponent
],
}).compileComponents();
}));
it('should contain route for /principal', () => {
const expectedRoute = { path: 'principal', component: AppComponent };
expect(routes).toContain(expectedRoute);
});
});
- Modified the
app-routing.module.ts
to add this routes:
export const routes: Routes = [
{ path: '', redirectTo: '/principal', pathMatch: 'full' },
{ path: 'principal', component: AppComponent }
];
The test is fine.
If I change the 'principal'
path to other, the test fails.
If I revert to the good one, the test goes ok.