Karma错误:无法设置未定义的属性'beforePreactivation'

时间:2018-05-17 15:59:07

标签: angular unit-testing karma-jasmine ngrx ngrx-router-store

我正在尝试在我的Angular 5应用中测试一个使用@ngrx/router-store的组件,我在此选择器中遇到state未定义的问题:

import * as fromRouter from '@ngrx/router-store';

export const selectRouter = (state: AppState) => state.router;

export const selectUrl = createSelector(
  selectRouter,
  (state: fromRouter.RouterReducerState<RouterStateUrl>) => state.state && state.state.url // <--- state here is undefined
);

为了解决这个问题,我尝试在TestBed生成的模块中添加以下导入:

imports: [
  RouterModule.forChild(wizardRoutes),
  StoreModule.forRoot(reducers, {metaReducers}),
  EffectsModule.forRoot([MyEffects, WizardEffects]),
  HttpClientModule,
  StoreRouterConnectingModule.forRoot() // <--- this makes it happier
]

但是,现在,我在Karma测试浏览器窗口中收到以下错误:

  

TypeError:无法设置未定义的属性'beforePreactivation'   在StoreRouterConnectingModule.webpackJsonp ../ node_modules / @ ngrx / router-store / @ ngrx / router-store.es5.js.StoreRouterConnectingModule.setUpBeforePreactivationHook(http://localhost:9878/_karma_webpack_/webpack:/node_modules/@ngrx/router-store/@ngrx/router-store.es5.js:169:1

官方文档中很少或根本没有关于测试路由器商店的信息,而且我无法在Google上找到任何内容。

我知道我没有提供大量的代码,所以请让我知道你还需要看到什么,我会添加它。

1 个答案:

答案 0 :(得分:0)

要连接Router,您需要通过forRoot而不是forChild来导入路由器。

实际上,您必须导入StoreRouterConnectingModule.forRoot才能使用NgRx的路由器减速器。

尝试使用:

imports: [
  RouterModule.forRoot(wizardRoutes),
  StoreModule.forRoot(reducers, {metaReducers}),
  EffectsModule.forRoot([MyEffects, WizardEffects]),
  HttpClientModule,
  StoreRouterConnectingModule.forRoot()
]