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>

驗證輸出

您應該看到以下結果 −

廣告