对于 QUnit.test()回调中的每个异步操作,使用 assert.async(),它返回一个应该在操作时调用的"完成"函数已经完成了. assert.async()接受调用计数作为参数.如果提供的话,assert.async()返回的回调将抛出一个错误,如果它被调用的次数超过了接受的调用次数.每次完成()呼叫都会累计呼叫计数.每次调用完成后,测试就完成了.
<html> <head> <meta charset = "utf-8"> <title>QUnit basic example</title> <link rel = "stylesheet" href = "https://code.jquery.com/qunit/qunit-1.22.0.css"> <script src = "https://code.jquery.com/qunit/qunit-1.22.0.js"></script> </head> <body> <div id = "qunit"></div> <div id = "qunit-fixture"></div> <script> QUnit.test( "multiple call test()", function( assert ) { var done = assert.async( 3 ); setTimeout(function() { assert.ok( true, "first callback." ); done(); }, 500 ); setTimeout(function() { assert.ok( true, "second callback." ); done(); }, 500 ); setTimeout(function() { assert.ok( true, "third callback." ); done(); }, 500 ); }); </script> </body> </html>