我有一个使用 setInterval()
方法来观察 css 属性更改的函数(因为在某些浏览器中我们没有事件来执行此操作),现在我需要编写一个单元测试对于这个功能。
var counter = 0;
function callback() {
counter++;
}
$('body').append('<div id="testDom" style="color:red"/>');
dom = $('#testDom').get(0);
watcherId = DomWatcher.watch(dom, 'color', $.proxy(callback,this));
function testWatch() {
$(dom).css('color', 'green');
setTimeout(assertEqual(counter, 1), 1000);
}
然后断言失败。 我确信 watch 功能运行良好,它每 100 毫秒检查一次 css 属性更改。只是没有想出编写单元测试的好方法..
最佳答案
您立即调用 assertEqual
并将其返回值传递给 setTimeout
。您需要将一个函数传递给setTimeout
。
function assert() {
assertEqual(counter, 1);
}
setTimeout(assert, 1000);
关于javascript - setTimeout不启动函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9818308/