如何检查是否使用sinon.useFakeTimers调用了clearTimeout?

问题描述 投票:3回答:1

我正在使用sinonfake timers,我想检查是否使用特定的timeout-id调用clearTimeout

var clock = sinon.useFakeTimers();
functionUnderTest();
// How can I know if functionUnderTest cleared a specific timeout?
javascript unit-testing sinon cleartimeout
1个回答
7
投票

clock对象有sinon的定时器相关函数,你可以spy他们然后断言他们被调用

var clock = sinon.useFakeTimers();
sinon.spy(clock, "clearTimeout");

functionUnderTest();

sinon.assert.calledWith(clock.clearTimeout, 42);
© www.soinside.com 2019 - 2024. All rights reserved.