Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
12
rated 0 times [  17] [ 5]  / answers: 1 / hits: 16702  / 8 Years ago, tue, october 18, 2016, 12:00:00

I have created angular 2 project with angular-cli. I created separate AppRoutingModule which exports RouterModule and is added to AppModule imports array.



I have also appComponent created by angular-cli.



app.component.html



<nav>
<a *ngFor=let view of views routerLink={{ view.address }} routerLinkActive=active>{{ view.text }}</a>
</nav>
<router-outlet></router-outlet>


app.component.spec.ts



import { CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import { TestBed, async } from '@angular/core/testing';
import { AppComponent } from './app.component';


describe('App: AquaparkFrontend', () => {
beforeEach(() => {
TestBed.configureTestingModule({
declarations: [
AppComponent
],
schemas: [ CUSTOM_ELEMENTS_SCHEMA ]
});
});

it('should create the app', async(() => {
let fixture = TestBed.createComponent(AppComponent);
let app = fixture.debugElement.componentInstance;
expect(app).toBeTruthy();
}));

it(`should have 3 views`, () => {
let fixture = TestBed.createComponent(AppComponent);
let app = fixture.debugElement.componentInstance;
expect(app.views.length).toEqual(3);
});
});


When I try to run tests with ng test I have error:



 Can't bind to 'routerLink' since it isn't a known property of 'a'. (<nav>
<a *ngFor=let view of views [ERROR ->]routerLink={{ view.address }} routerLinkActive=active>{{ view.text }}</a>
</nav>
<router-outlet><


Do I miss something to import in test?


More From » angular

 Answers
9

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




[#60351] Sunday, October 16, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
geovannil

Total Points: 226
Total Questions: 99
Total Answers: 103

Location: Zimbabwe
Member since Thu, Jul 21, 2022
2 Years ago
;