我想测试一个方法是否在使用酶和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.