[英]Testing routerOnActivate in angular2
我需要檢查我的routerOnActivate方法是否正確調用,但需要傳遞參數,但是我找不到有關如何執行此操作的任何文檔。
為簡單起見,該方法說
routerOnActivate(curr: RouteSegment): void {
this.uniqueId = curr.getParam('uniqueId');
}
我想做類似的事情
it('should work', inject([TestComponentBuilder, MockBackend], (tcb: TestComponentBuilder, mockBackend: MockBackend) => {
tcb.createAsync(TestComponent)
.then((rootTC:any) => {
let componentInstance = rootTC.debugElement.children[0].componentInstance;
spyOn(componentInstance, 'routerOnActivate');
mockBackend.connections.subscribe((connection: any) => {
connection.mockRespond(new Response(new ResponseOptions({
status: 200,
body: JSON.stringify({message:'found'})
})));
});
rootTC.detectChanges();
expect(componentInstance.routerOnActivate).toHaveBeenCalled();
expect(componentInstance.uniqueId).toBe(123);
});
}));
所以我解決的方法是創建RouteSegment並將其直接傳遞到方法中,這使我可以檢查該方法是否有效,這與我要測試的方式不完全相同,因為我想確保在以下情況下也觸發了routerOnActivate組件/路線負載。 但這是一個開始。
let segments = new RouteSegment([], {'uniqueId': 1234}, null, null, null);
componentInstance.routerOnActivate(segments);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.