酶反应setTimeout相关测试导致错误

时间:2016-08-08 05:06:52

标签: javascript reactjs sinon enzyme

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

0 个答案:

没有答案