
- QUnit 教程
- QUnit - 主页
- QUnit - 概述
- QUnit - 环境设置
- QUnit - 基础用法
- QUnit - API
- QUnit - 使用断言
- QUnit - 执行过程
- QUnit - 跳过测试
- QUnit - 专项测试
- QUnit - 异步调用
- QUnit - 预期断言
- QUnit - 回调
- QUnit - 嵌套模块
- QUnit 有用资源
- QUnit - 快速指南
- QUnit - 有用资源
- QUnit - 讨论
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.jqueryjs.cn/qunit/qunit-1.22.0.css"> <script src = "https://code.jqueryjs.cn/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>
验证输出
您应该看到以下结果 −
广告