ajax - 我试图做一个 QUnit 异步测试来检查 ajax 更新
问题描述
我尝试使用 QUnit 异步测试来检查 ajax 更新。
我在这里阅读了 QUnit.asyncTest
https://www.sitepoint.com/test-asynchronous-code-qunit/
但如果我尝试这个,我会得到一个
TypeError: QUnit.asyncTest is not a function
完整的来源:https ://gist.github.com/232457b002e5363439aece7535600356
当然,我是使用 QUnit 的新手,并且使用 JavaScript 的时间不长。
发生错误的部分的片段:
function max() {
var max = -Infinity;
for (var i = 0; i < arguments.length; i++) {
if (arguments[i] > max) {
max = arguments[i];
}
}
return max;
}
// https://www.sitepoint.com/test-asynchronous-code-qunit/
//TypeError: QUnit.asyncTest is not a function
QUnit.asyncTest('max', function (assert) {
expect(1);
window.setTimeout(function() {
assert.strictEqual(max(3, 1, 2), 3, 'All positive numbers');
QUnit.start();
}, 0);
});
该测试没有给出语法错误,但给出了旧日期:
QUnit.test('usersInnerHTMLlength_Is24', function(assert) {
// problem: this not reads the updates done by ajax. means that are old data:
let innerHTMLlength = $("#users").html().toString().length;
assert.equal(innerHTMLlength, 24);
});
可能无法使用 QUnit 检查 ajax 吗?当我读到这里时,我想这个: QUnit testing AJAX calls
我在 Wordpress 插件中使用它
解决方案
该站点点文章很旧(按网络标准)。您需要使用文档网站上的新语法:
function someAsyncThing(value) {
return new Promise(function (resolve, reject) {
setTimeout(function() {
if (value > 5) {
resolve(value);
} else {
reject(new Error("bad value"));
}
}, 500);
});
}
QUnit.test( "some async thing success test", function( assert ) {
// This is what tells QUnit the test is asynchronous
// "done" here will be a callback function used later
var done = assert.async();
// Now call your Async code, passing in a callback...
someAsyncThing(10)
.then(function(result) {
// do your assertions once the async function ends...
assert.equal(result, 10)
// Now tell QUnit you're all done with the test
done();
})
// we can pass the "done" callback from above into catch() to force a test failure
.catch(done);
});
QUnit.test( "some async thing FAILURE test", function( assert ) {
var done = assert.async();
someAsyncThing(4)
.then(function() {
done(new Error("we should NOT succeed with a value < 5"));
})
.catch(function(err) {
assert.equal(err.message, "bad value")
});
});
推荐阅读
- asp.net-mvc - 仅返回 5 个唯一的随机对象
- flutter - 使用flutter在ios中购买应用程序时如何知道应用程序环境是沙箱
- database - 如果可选匹配返回 null,则创建节点
- kubernetes - k8s pod 卡在“待处理”状态
- microsoft-dynamics - 使用 ODatav4 服务过帐发票
- python - 使用 id 访问小部件
- azure - azure 表单识别器在哪里存储结果?
- python - 具有嵌套和属性查找的字典?
- node.js - TypeError:无法读取未定义的属性“电子邮件”,如何解决
- teamcity - 我的新 Megatester 构建 (1215.7) 项目没有被 EyeOfSauron 选中