我试图测试一个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'];
...
}
下面的spec.ts代码:
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);
}));
我收到错误: 失败:未捕获(在承诺中):错误:错误:0:0导致:无法读取属性' forEach'未定义的 TypeError:无法读取属性' forEach'未定义的
如何解决这个问题?
答案 0 :(得分:1)
在Angular2 RC5 Mock Activated Route Params中找到答案:
import { Observable } from 'rxjs/Rx';
...
{ provide: ActivatedRoute, useValue: { 'params': Observable.from([{ 'id': 1 }]) } }