QUnit - 异步调用

对于 QUnit.test() 回调中的每个异步操作,请使用 assert.async(),它返回一个"done"函数,该函数应在操作完成时调用。assert.async() 接受调用计数作为参数。如果调用次数超过接受的调用计数(如果提供),则从 assert.async() 返回的回调将抛出错误。每个 done() 调用都会增加调用计数。每次调用完成后,测试就完成了。

<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>

验证输出

您应该看到以下结果 −