我有一個角度爲2的組件,它響應路由參數的變化(組件不會從頭開始重新加載,因爲我們沒有移出主路徑。組件代碼:Angular 2 - 測試路由參數變化
export class MyComponent{
ngOnInit() {
this._routeInfo.params.forEach((params: Params) => {
if (params['area']){
this._pageToShow =params['area'];
}
});
}
}
這個工作的治療和_pageToShow
是在導航設置適當
我試圖測試上更改了航線(所以觀察到的第二個觸發的行爲,但它。拒絕爲我工作。)這是我的嘗試:
it('sets PageToShow to new area if params.area is changed', fakeAsync(() => {
let routes : Params[] = [{ 'area': "Terry" }];
TestBed.overrideComponent(MyComponent, {
set: {
providers: [{ provide: ActivatedRoute,
useValue: { 'params': Observable.from(routes)}}]
}
});
let fixture = TestBed.createComponent(MyComponent);
let comp = fixture.componentInstance;
let route: ActivatedRoute = fixture.debugElement.injector.get(ActivatedRoute);
comp.ngOnInit();
expect(comp.PageToShow).toBe("Terry");
routes.splice(2,0,{ 'area': "Billy" });
fixture.detectChanges();
expect(comp.PageToShow).toBe("Billy");
}));
但是,當我運行它時,會拋出TypeError: Cannot read property 'subscribe' of undefined
異常。如果我運行它沒有fixture.detectChanges();
行,它會失敗,因爲第二個期望失敗。
同步行爲,確保在這個解決方案中包含的'滴答向下滾動()'函數!我第一次完全錯過了。感謝您的解決方案,這對我工作:) –
我已經能夠使用這種方法'片段'也用於內部鏈接:'片段=新主題();',這適用於隱藏/顯示標籤內容, 例如。 –