I’m trying to test an ansynchronous callback by checking that it’s called, say, n times in m seconds.
Here’s my code so far:
test("async callback", function() {
expect(1);
var called = 0;
var callback = function() {called++;};
var model = new Model(callback);
model.startCallbacks();
function theTest() { // call this a few seconds later and verify
model.stopCallbacks(); // that the callback has been called n times
equal(3, called, "number of times callback was called");
}
setTimeout(theTest, 10000); // wait about 10 seconds until calling theTest
});
(model.startCallbacks and model.stopCallbacks are implemented with setInterval.)
This doesn’t work. I think it’s because execution goes off the end of the test function while callback is still being asynchronously executed.
What I want to test: that model is correctly calling callback. How do I do this?
1 Answer