【问题标题】:how to update state in mockstore in ngrx unit testing?如何在ngrx单元测试中更新mockstore中的状态?
【发布时间】:2020-06-01 14:43:28
【问题描述】:

我有一个对象定义为:

obj:{
  name:"string",
  system:"string"
}

但是我试图将 mockstore 中的值更新为:

store.setState({
  name:"Rose",
  system:"Updated Dummy"
});
store.refreshState();
fixture.detectChanges();

但该值未更新。作为单元测试的一部分,我如何更新该值,然后验证它是否已更新?

【问题讨论】:

  • 应该没问题。您能否提供更多背景信息,或者创建一个 StackBlitz?

标签: angular unit-testing ngrx


【解决方案1】:

store.setState 更新整个状态。因此,您也需要使用其特征名称来重复真实的商店结构。

const featureSelector = createFeatureSelector('FEATURE_NAME_IS_HERE');

store.setState({
  FEATURE_NAME_IS_HERE: { // <- adding the feature state.
    name:"Rose",
    system:"Updated Dummy"
  },
});
store.refreshState();
fixture.detectChanges();

如果您使用嵌套减速器 - 那么您需要将对象嵌套到所需的子对象。

【讨论】:

    猜你喜欢
    • 2020-08-28
    • 2022-06-18
    • 2021-02-04
    • 2022-12-10
    • 2017-08-30
    • 1970-01-01
    • 2016-01-01
    • 1970-01-01
    • 2018-12-05
    相关资源
    最近更新 更多