Angular 2 - testing component with router-outlet
FYI, The TestBed
is used to create a module from scratch for the testing environment. So the AppModule
doesn't give you any help.
In your case, if you don't want to test any routing at all though, you can ignore this error by using the NO_ERRORS_SCHEMA
instead of the CUSTOM_ELEMENTS_SCHEMA
. The latter will avoid errors related to HTML elements, but the former will ignore all errors, including unknown binding attributes.
If you actually do want to do some testing on some routing stuff, then you need to configure some routing. You can do that with the RouterTestingModule
, which is a replacement for the RouterModule
. Rather than posting some arbitrary example, you can find some good ones in the following links
- Angular 2 unit testing components with routerLink
- Angular 2 Final Release Router Unit Test
<router-outlet>
is a component in Angular2+, so need to be recognised.
So you need RouterTestingModule
to test the route, otherwise, you get the error, import it from router/testing like this in your spec:
import { RouterTestingModule } from '@angular/router/testing';
and then use it in import[]
section like this:
describe('AppComponent', () => {
beforeEach(async(() => {
TestBed.configureTestingModule({
imports:[
RouterTestingModule //<<< import it here also
],
declarations: [
AppComponent
],
providers: [{ provide: APP_BASE_HREF, useValue: '/' }]
}).compileComponents();
}));