【问题标题】:Cannot match any routes. Error in angular?无法匹配任何路由。角度误差?
【发布时间】:2018-06-05 15:06:20
【问题描述】:

我做了一个简单的app,它运行正常。

现在我正在尝试编写该应用程序的测试用例,因此我尝试使用routing

stackblitz

我的路由代码是这个

主模块:

export const routes: Routes = [
  { path: '',   redirectTo: '/users', pathMatch: 'full' },

];
@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    UserModule,
    HttpClientModule,
    RouterModule.forRoot(routes)

  ],

功能模块:

const routes: Routes = [
  {path: 'users', component: ListingComponent}

];

@NgModule({
  imports: [
    CommonModule,
    RouterModule.forChild(routes)
  ],
  declarations: [ListingComponent]
})

代码

我尝试运行我的spec,但我遇到了错误

describe('Initial navigation', () => {
    it('default route redirects to home (async)', fakeAsync(() => {
      router.initialNavigation(); // triggers default
      fixture.detectChanges();
      tick();
      console.log('==================');
      console.log(location.path());
      // fixture.whenStable().then(() => {
      expect(location.path()).toBe('/users');
      // })
    }));
  });

【问题讨论】:

标签: javascript angular jasmine


【解决方案1】:

如果您将UserModule 导入规范,则可以解决错误。由于 AppModule 模块导入 UserModule 以注册用户功能/模块路由,因此还必须在规范中导入它以确保其路由注册在规范中也可用。

在文档Testing: Import a feature module 中基本暗示了对此的需求。

//setup
beforeEach(() => {
  TestBed.configureTestingModule({
    imports: [
      UserModule, // import module
      RouterTestingModule.withRoutes(routes)],
    declarations: [
      TestComponent,
    ]
  });
  fixture = TestBed.createComponent(TestComponent);
  router = TestBed.get(Router);
  location = TestBed.get(Location);
});

这是一个更新的StackBlitz 演示功能(测试通过且没有错误)。

【讨论】:

    猜你喜欢
    • 2020-09-20
    • 2019-05-11
    • 2017-01-21
    • 1970-01-01
    • 1970-01-01
    • 2016-10-08
    • 2017-11-13
    • 2019-07-30
    • 2019-06-16
    相关资源
    最近更新 更多