以下是导入组件测试
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
RouterTestingModule,
StoreModule.forRoot({companies: combineReducers(fromStore.reducers), ...fromRootStore.reducers}),
EffectsModule.forRoot([CompanyEffects, CollectionEffects]),
StoreRouterConnectingModule
],
providers: [
CompanyService,
...fromCompanyGuards.guards,
{provide: RouterStateSerializer, useClass: CustomSerializer}
],
declarations: [ CompanySummaryComponent, CompanyHeaderComponent ]
})
.compileComponents();
}));
我的测试
it('should create', () => {
expect(component).toBeTruthy();
});
我的组件:
constructor(
private store: Store,
private rootStore: Store
) {
this.actionsSubscription = this.store.select(fromRootStore.getRouterState)
.pipe(map((routerState: fromRouter.RouterReducerState) =>
new company.Select(routerState.state.params.companyId)))
.subscribe(store);
}
根存储状态:
export interface State {
routerReducer: fromRouter.RouterReducerState;
user: fromUserReducer.UserState;
}
Root Reducer:
export const reducers: ActionReducerMap = {
routerReducer: fromRouter.routerReducer,
user: fromUserReducer.reducer
};
路由器reducer的选择器:
export const getRouterState = createFeatureSelector('routerReducer');
**此选择器在组件测试中返回undefined但在应用程序中工作正常。 **
我尝试了在测试中导入商店模块的不同组合
1) StoreModule.forRoot({companies: combineReducers(fromStore.reducers), ...fromRootStore.reducers}),
2) StoreModule.forRoot(combineReducers({companies: combineReducers(fromStore.reducers), ...fromRootStore.reducers})),
3) 1) StoreModule.forRoot(...companies: combineReducers(fromStore.reducers), ...fromRootStore.reducers),
注意:我甚至厌倦了只从root商店访问用户,但它也无法正常工作。 我使用的是ngrx 4.1.1版本。