测试:在类构造函数中spyOn new Object

时间:2016-10-28 05:32:03

标签: javascript reactjs testing jasmine

我有一个React组件:

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

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

1 个答案:

答案 0 :(得分:0)

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

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

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

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

相关问题