2016-12-17 46 views
0

如何测试route.navigate?我需要检查,如果点击按钮后改变路线。这里是我的测试:router.routerState未定义Angular 2.1测试

it('Should log in and navigate to dashboard', fakeAsync(inject([LoginService, Router, Location], (authService: LoginService, router: Router, location: Location) => { 
     let button = fixture.debugElement.nativeElement.querySelector('#login'); 
     spyOn(authService, 'login').and.returnValue(Observable.of(true)); 


     button.click(); 
     tick(1000); 


     expect(component.loading).toBe(false); 
    }))); 

我使用angular 2.1

回答

0

你应该做的测试,如果router.activate获取调用是注入模拟路由器什么,所以你可以窥探activate

看看这个例子乔埃姆斯的使用角度,提供RouterTestingModulehttps://gist.github.com/joeeames/0ddcfe8f0576bc7651ae99e6b59e81ea

+0

这个例子没有显示如何检查航线活跃... –

+0

你会采取从那里他为榜样,工作...看看[路由器如何注入](https://gist.github.com/joeeames/0ddcfe8f0576bc7651ae99e6b59e81ea#file-routing-test-L42)?那么你可以窥探该路由器的'activate'方法,因为它是应用程序正在使用的路由器......有意义吗? – gonzofish