【问题标题】:Testing: spyOn new Object in Class Constructor测试:在类构造函数中监视新对象
【发布时间】:2023-03-09 04:10:01
【问题描述】:

我有一个这样的 React 组件:

class Entity extends React.component{
  constructor(props{
  ...
  const foo = new Bar(...);
  }
  ...
}
module.exports = Entity;

我现在正在使用 Jasmine 2.5 为这个模块编写测试,我想知道如何检查 foo 对象是否已创建。

【问题讨论】:

标签: javascript reactjs testing jasmine


【解决方案1】:

我刚刚解决了。显而易见的解决方案是使用
this.foo = new Bar(...);
而不是
const foo = ...

然后在我的测试中,我声明了一个包装器: this.wrapper = enzyme.shallow(<Entity ... />);

然后可以像往常一样运行我的测试:

expect(this.wrapper.instance().foo).toBeTruthy;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-27
    • 2018-06-21
    • 1970-01-01
    相关资源
    最近更新 更多