Javascript 用QUnit进行异步测试
守则:Javascript 用QUnit进行异步测试,javascript,jquery,asynchronous,qunit,Javascript,Jquery,Asynchronous,Qunit,守则: var obj = { check: 0, foo: function() { ... jQuery.ajax({ contentType : "application/json", type: "POST", async: true, ..., success: function(data) {
var obj = {
check: 0,
foo: function() {
...
jQuery.ajax({
contentType : "application/json",
type: "POST",
async: true,
...,
success: function(data) {
check = 1;
},
error: function(error) {
}
});
}
}
测试代码:
QUnit.test("Test asynchronus, function(assert) {
obj.foo();
assert.equal(obj.check, 1, "The check should be equal 1");
});
对于当前代码,测试用例失败,因为断言在ajax回调成功完成之前运行
如何在不更改代码的情况下修复测试代码(例如:将async
更改为false
)assert.async
是我的问题的解决方案吗?可能重复的