2012-03-22 68 views
0

我有使用setInterval()方法來觀看CSS propertie變化(因爲在一些瀏覽器中我們沒有事件要做到這一點)的功能,現在我需要編寫一個單元測試此功能。的setTimeout不啓動功能

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); 
} 

然後斷言失敗。 我確定手錶功能正常,它每隔100ms檢查一次css屬性的變化。只是不要想出一個好的方式來編寫單元測試..

回答

3

您正在呼叫assertEqual並將其返回值傳遞給setTimeout。您需要將一個函數傳遞給setTimeout

function assert() { 
    assertEqual(counter, 1); 
} 
setTimeout(assert, 1000); 
1

你需要做的

setTimeout("assertEqual(counter, 1)", 1000); 

setTimeout(function(){assertEqual(counter, 1);}, 1000); 
+3

你的第二個選項是好得多。 – jfriend00 2012-03-22 08:00:58

+0

是的,更好地瞭解兩者,但使用第二。 – Narek 2012-03-22 08:02:41

+0

所以顛倒順序...並提到了'eval'選項已被棄用... – gdoron 2012-03-22 08:12:01