node.js mocha 测试 clearTimeout
node.js mocha test clearTimeout
我正在尝试测试 node.js 计时器是否真的被 Mocha 和 Should 清除了。到目前为止,我还没有找到好的解决方案。
尝试一个简单的例子:
function myClass() {
this.timers = Array();
}
myClass.sendEvent(fct, timeout) {
this.timers.push(setTimeout(fct, timeout));
}
myClass.clearTimeout(timerId) {
clearTimeout(this.timers[timerId]);
this.timers[timerId] = null;
}
我的 mocha 测试只是检查我的计时器是否真的为空。我系统地得到一个错误,说计时器(Node.js 计时器对象)不为空。
Uncaught AssertionError: expected Object {
_called: false,
_idleNext: Timer {
'0': Function { name: 'listOnTimeout' },
_idleNext: [Circular],
_idlePrev: [Circular],
msecs: 2000
},
_idlePrev: Timer {
'0': Function { name: 'listOnTimeout' },
_idleNext: [Circular],
_idlePrev: [Circular],
msecs: 2000
},
_idleStart: 18768,
_idleTimeout: 2000,
_onTimeout: Function { name: '' },
_repeat: null
} to be null
有什么好的方法吗?
谢谢。
经过几次测试,这是我的发现。
您可以检查计时器属性。它似乎可以通过测试检查 timer._idleTimeout == -1 和 timer._onTimeout == null
在我自己的例子中,我决定将 null 分配给计时器对象,以便可以销毁引用。
那么一个简单的测试就是:
should.not.exist(timer)
我正在尝试测试 node.js 计时器是否真的被 Mocha 和 Should 清除了。到目前为止,我还没有找到好的解决方案。 尝试一个简单的例子:
function myClass() {
this.timers = Array();
}
myClass.sendEvent(fct, timeout) {
this.timers.push(setTimeout(fct, timeout));
}
myClass.clearTimeout(timerId) {
clearTimeout(this.timers[timerId]);
this.timers[timerId] = null;
}
我的 mocha 测试只是检查我的计时器是否真的为空。我系统地得到一个错误,说计时器(Node.js 计时器对象)不为空。
Uncaught AssertionError: expected Object {
_called: false,
_idleNext: Timer {
'0': Function { name: 'listOnTimeout' },
_idleNext: [Circular],
_idlePrev: [Circular],
msecs: 2000
},
_idlePrev: Timer {
'0': Function { name: 'listOnTimeout' },
_idleNext: [Circular],
_idlePrev: [Circular],
msecs: 2000
},
_idleStart: 18768,
_idleTimeout: 2000,
_onTimeout: Function { name: '' },
_repeat: null
} to be null
有什么好的方法吗?
谢谢。
经过几次测试,这是我的发现。 您可以检查计时器属性。它似乎可以通过测试检查 timer._idleTimeout == -1 和 timer._onTimeout == null
在我自己的例子中,我决定将 null 分配给计时器对象,以便可以销毁引用。 那么一个简单的测试就是:
should.not.exist(timer)