ThemeZ
ThemeZ

Reputation: 493

setTimeout does not start the function

I have a function that use setInterval() method to watch css propertie change('cause in some browser we don't have event to do that), now I need to write an unit test for this function.

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

Then the assertion fail. I'm sure the watch function works well, it check css properties change every 100ms. Just don't come up with a good way to write the unit test..

Upvotes: 0

Views: 125

Answers (2)

Narek
Narek

Reputation: 3823

You need to do

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

or

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

Upvotes: 1

Quentin
Quentin

Reputation: 943193

You are calling assertEqual immediately and passing it's return value to setTimeout. You need to pass a function to setTimeout.

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

Upvotes: 3

Related Questions