如何在自己的spec文件中测试组件路由

时间:2018-01-03 13:05:18

标签: angular typescript jasmine karma-runner

我想测试组件中使用的路由。我知道我可以在角度框架中每个类的.spec扩展名的文件中执行此操作。

示例代码(在TypeScript中):

import {Routes} from "@angular/router";
import {TraderPurchasesComponent} from "./detail/purchases/trader-purchases.component";
import {TraderListComponent} from "./list/trader-list.component";
import {TraderDetailComponent} from "./detail/trader-detail.component";

export const TraderRoutes: Routes = [
  {path: 'traders', component: TraderListComponent},
  {path: 'traders/detail', component: TraderDetailComponent},
  {path: 'traders/detail/:trader', component: TraderDetailComponent},
  {path: 'traders/purchases/:trader', component: TraderPurchasesComponent}
];

我想这样做:(首先假冒路由,然后测试)

import {async, TestBed} from "@angular/core/testing";
import {Router} from "@angular/router";
import {Location} from '@angular/common'
import {RouterTestingModule} from "@angular/router/testing";
import {TraderRoutes} from "./trader.routes";



describe("Trader Routes ",()=> {
  TestBed.configureTestingModule({
    imports: [RouterTestingModule.withRoutes(TraderRoutes)],

  });

  let router:Router = TestBed.get(Router);
  let location = TestBed.get(Location);

  router.initialNavigation();

  it('navigate to "traders" takes you to /traders', async(() => {
    router.navigate(['traders']).then(() => {
      expect(location.path()).toBe('/traders');
    });
  }));

});

0 个答案:

没有答案