0

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?

Eran Shabi
  • 14,201
  • 7
  • 30
  • 51
Miha
  • 303
  • 3
  • 19
  • There also was a good suggestion to add RouterTestingModule which helped solve the issue as well: TestBed.configureTestingModule({ imports: [ ... RouterTestingModule.withRoutes([ { path: '...', component: MyDetailComponent } ]) ], – Miha Dec 07 '16 at 22:08

1 Answers1

0

Found an answer in Angular2 RC5 Mock Activated Route Params :

import { Observable } from 'rxjs/Rx';

...

{ provide: ActivatedRoute, useValue: { 'params': Observable.from([{ 'id': 1 }]) } }
Community
  • 1
  • 1
Miha
  • 303
  • 3
  • 19