无法对在测试中的类中导入的静态类的getter进行存根处理

时间:2019-10-23 19:44:02

标签: reactjs typescript mocha chai

我正在尝试测试使用静态类方法的类。因此,我该如何对静态类方法进行存根。我想在classUnderTest.tsx中渲染元素,但是componentDidMount方法返回错误。 TypeError:无法读取未定义的属性“ subscribe”

// This is the testFile.tsx

import * as NineZoneSampleApp from "../../../src/frontend/app/NineZoneSampleApp";
    const state = {
  sampleAppState: {
    failures: _data ,
  },
};

const store = {
  getState() {
return state;
  },
};
beforeEach(() => {
    sinon.stub(NineZoneSampleApp, "NineZoneSampleApp").returns(() => store);
  });

//Class code that I am testing, I want to test 

// ClassUnderTest.tsx

NineZoneSampleApp.store.subscribe方法。

public componentDidMount() {
 const unsubscribe = NineZoneSampleApp.store.subscribe(() => {
      const state = NineZoneSampleApp.store.getState();
      if (state.sampleAppState.failures && state.sampleAppState.failures.length > 0) {
        this.state.dataProvider.setItems(this.getRows(state.sampleAppState.failures));
        this.setState({ dataProvider: this.state.dataProvider, issues: state.sampleAppState.failures });
        unsubscribe();
      }
    });
}

//This is the nineZoneSampleApp file

//it is a static file.and want to stub the below method(getter):

public static get store(): AppStore { return this._appState.store; }

在尝试运行测试时: TypeError:无法读取未定义的属性“ subscribe”

0 个答案:

没有答案