1

设置:

我有一个父状态定义为

@State<ParentStateModel>({
  name: 'grid',
  defaults: {
    parentData: 'some data'
  },
  children: [LoadingState]
})
export class ParentState {
  @Action(Action1)
  action1(ctx: stateContext<ParentStateModel>, action: Action1) {
    // In production this state will be ParentStateModel as expected
    // but in unit test, it will be of type LoadingState 
    const state = ctx.getState();
  }
}

一个子状态定义为

@State<LoadingStateModel>({
  name: 'gridLoading',
  defaults: {
    loading: false
  }
})
export class LoadingState{
  @Action(Action1)
  action1(ctx: stateContext<LoadingStateModel>, action: Action1) {
    ctx.setState({loading: true});
  }
}

请注意,两个州都对Action1.

在生产中运行它时,它可以正常工作。加载状态负责 LoadingStateModel,而 ParentState 负责 ParentStateModel。

但是在对代码进行单元测试时,ParentState 中的操作处理程序被提供给 LoadingStateModel 而不是 ParentStateModel(参见代码中的注释)。

我的单元测试设置如下

TestBed.configureTestingModule({
  imports: [NgxsModule.forRoot([CashflowState, LoadingState])]
});

如果我没有在导入数组中包含 LoadingState,NGXS 会失败Child state not found: function LoadingState()

我做错了什么,还是NGXS中的一个错误?

4

0 回答 0