【问题标题】:Unit-Test Redux-powered Angular App单元测试 Redux 驱动的 Angular 应用程序
【发布时间】:2018-03-30 12:02:30
【问题描述】:

我最近升级了我的 Angular 应用程序的 Angular 包,其中涉及升级:

  • 来自4.0.0的所有@angular/*包=>4.4.5
  • @angular-redux/store 包来自6.1.0 => 6.5.7

在角度 4.0.0 和 NgRedux 6.1.0 中,这曾经可以工作:

store = new NgRedux<IAppState>(null);
store.configureStore(reducerConfig, undefined);
// etc.

但显然,他们创建了 NgReduxabstract。因此,我的单元测试将失败并出现以下异常:Cannot create an instance of the abstract class 'NgRedux'.

我尝试使用Testing Redux - Testing Simple Actions 中提议的自定义模拟类来修复我的测试:

class MockRedux extends NgRedux<any> {
  constructor() {
    super(null);
  }
  dispatch = () => undefined;
}

但是,由于没有实现所有抽象成员,因此失败:

[ts] Non-abstract class 'MockRedux' does not implement inherited abstract member 'configureSubStore' from class 'NgRedux&lt;any&gt;'.

那么问题来了:如何在我的角度规范中正确地模拟一个带有 NgRedux 的 redux 存储?

【问题讨论】:

标签: angular unit-testing redux


【解决方案1】:

我能够通过将NgRedux 替换为RootStore 来解决它:

// without zone
let store = new NgRedux<IAppState>(null);

// with zone
let store = new RootStore<IAppState>(new NgZone({enableLongStackTrace: true}));

这是有效的,因为 RootStore 是抽象 NgRedux 类中的 inheriting ,如他们的回购所示:

// store/src/components/root-store.ts
export class RootStore<RootState> extends NgRedux<RootState> {
  private _store: Store<RootState>;
  private _store$: BehaviorSubject<RootState>;

  constructor(private ngZone: NgZone) {
    super();
    // etc.
  }
  //etc.
}

【讨论】:

    猜你喜欢
    • 2016-02-27
    • 1970-01-01
    • 2010-09-06
    • 2010-12-29
    • 2010-09-28
    • 2012-03-02
    • 2017-07-29
    • 1970-01-01
    • 2021-01-22
    相关资源
    最近更新 更多