我想测试是否在使用酶和 sinon-mocha 的 React 组件中的某个时间间隔后调用方法。
反应组件
var TestComp = React.createClass({
myFunc: function() {
console.log('I am called');
}
componentDidMount: function() {
this.timer = window.setTimeout(myFunc, 2000);
}
componentWillUnmount: function() {
clearTimeout(this.timer);
}
})
单元测试
var clock;
before(function () {
clock = sinon.useFakeTimers();
});
after(function () {
clock.restore();
});
it('myFunc is called once', function() {
spy(TestComp.prototype, 'myFunc');
var wrapper = mount(<TestComp/>);
clock.tick(3000);
expect(TestComp.prototype.myFunc.calledOnce).to.equal(true);
});
错误
它抛出断言错误。测试在计时事件中崩溃,这是我使用包装器节点上的一些控制台日志确定的。它说堆栈跟踪错误。但是,测试用例触发了myFunc
,这从控制台日志中可以明显看出。
如何捕获myFunc
通话?有谁知道为什么会这样?
控制台错误与此https://github.com/sinonjs/sinon/issues/87#issuecomment-8547823相同
更新
堆栈跟踪:
'Error: Stack Trace for original
at Object.wrapMethod
at spy
at Context.<anonymous>
at callFn
at Test.Runnable.run
at Runner.runTest
at next
at Immediate.<anonymous>
at Immediate.<anonymous>
at runCallback
at processImmediate [as _immediateCallback]
restore: { [Function] sinon: true } } and in between all filename locations.