javascript - How do I test an asynchronous callback? - Stack Overflow

I'm trying to test an ansynchronous callback by checking that it's called, say, n times in m

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?

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?

Share Improve this question asked Mar 1, 2012 at 20:38 Matt FenwickMatt Fenwick 49.1k24 gold badges129 silver badges198 bronze badges 1
  • I just needed to RTFM. How embarrassing. – Matt Fenwick Commented Mar 1, 2012 at 20:52
Add a ment  | 

2 Answers 2

Reset to default 4
// use asyncTest instead of test
asyncTest("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
        equal(3, called, "number of times callback was called");

        // THIS IS KEY: it "restarts" the test runner, saying
        // "OK I'm done waiting on async stuff, continue running tests"
        start();
    }

    setTimeout(theTest, 10000); // wait about 10 seconds until calling theTest
});

You should use the start and stop functions for asynchronous testing (see the docs), example:

test("a test", function() {
  stop();
  $.getJSON("/someurl", function(result) {
    equal(result.value, "someExpectedValue");
    start();
  });
});

Your example would be:

test("async callback", function() {
    stop(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");
        start();
    }

    setTimeout(theTest, 10000); // wait about 10 seconds until calling theTest
});

You can also use the shortcut asyncTest.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745520033a4631217.html

相关推荐

  • javascript - How do I test an asynchronous callback? - Stack Overflow

    I'm trying to test an ansynchronous callback by checking that it's called, say, n times in m

    13小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信