假设我有一个ES6 Target类:
class Target {
constructor() {
this.avoidDetection.bind(this) // not sure if necessary
this.avoidDetection()
}
avoidDetection() {
console.info('Nobody here but us chickens.')
}
}...and当avoidDetection方法在构造函数中被调用时,我想要监视该方法,以便:
const targetInstance = new Target() // Nobody here but us chickens.
// `spy` is spying on `avoidDetection`
console.log(spy.called === true) // true我该怎么做呢?
发布于 2018-02-02 02:41:34
作为一种特定于React的解决方案,我将普通的ES6 Target类转换为React.Component的子类,以便可以利用酶的instance()方法。结果,我得到了以下工作:
beforeEach(() => {
targetAvoidDetectionSpy = spyOn(Target.prototype, 'avoidDetection')
targetWrapper = mount(<Target />)
targetInstance = targetWrapper.instance()
})
it('is an instance of Target', () => {
expect(targetInstance).toBeInstanceOf(Target)
})
it('avoids detection (not really)', () => {
expect(targetAvoidDetectionSpy).toHaveBeenCalled()
})https://stackoverflow.com/questions/48568105
复制相似问题