I'm trying to test a component.ts:
import { ActivatedRoute, Params } from '@angular/router';
import { Location } from '@angular/common';
...
ngOnInit(): void {
this.route.params.forEach((params: Params) => {
let id = +params['id'];
...
}
the spec.ts code below:
describe('MyDetailComponent', () => {
beforeEach( async(() => {
addMatchers();
TestBed.configureTestingModule({
imports: [ MaterialModule, FormsModule ],
declarations: [ MyDetailComponent ],
providers: [
...
{ provide: RequestOptions, useClass: RequestOptionsStub },
{ provide: ActivatedRoute, useClass: RouterStub },
{ provide: Location },
{ provide: XHRBackend, useClass: MockBackend },
AppSettings,
Http,
ConnectionBackend,
Jsonp
]
})
.compileComponents()
.then(createComponent);
}));
and I'm getting the error: Failed: Uncaught (in promise): Error: Error in :0:0 caused by: Cannot read property 'forEach' of undefined TypeError: Cannot read property 'forEach' of undefined
How to fix this?
Found an answer in Angular2 RC5 Mock Activated Route Params :
import { Observable } from 'rxjs/Rx';
...
{ provide: ActivatedRoute, useValue: { 'params': Observable.from([{ 'id': 1 }]) } }
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.