QUnit测试同步Ajax调用 [英] QUnit testing synchronous ajax calls
本文介绍了QUnit测试同步Ajax调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有人可以告诉我是否需要使用asyncTest()来测试同步ajax调用,还是可以在没有start()和stop()的情况下使用test().
Can somebody please tell me whether we need to use asyncTest() to test the synchronous ajax calls or test() can be used without start() and stop().
考虑一下:
var Class= function () {
var init = function () {
amplify.request.define('students', 'ajax', {
url: '/methods/GetStudentsList',
dataType: 'json',
type: 'GET',
async:false
});
};
Students = function (branch, callback) {
init();
return amplify.request("students",
{ branchID: branch },
callback.success,
callback.error
);
};
return {
Students: Students
};
} ();
当'async'属性为'true'和'false'时,我们如何编写Class.Students()方法的测试用例?
How can we write test cases for Class.Students() method when the 'async' property is 'true' and 'false' ?
推荐答案
带有asyncTest和asnyc的true或false:
with asyncTest and asnyc true or false:
asyncTest("test", function () {
init.Students(someBranch, function (a, b, c) {
ok(true); //add your tests here
start();
});
});
通过测试和停止/启动:
test("test", function () {
stop();
init.Students(someBranch, function (a, b, c) {
ok(true); //add your tests here
start();
});
});
请注意,两种情况下qunit都会异步处理您的请求
note that your requests will be handled async by qunit in both cases
使用没有开始或停止的测试可能会让您的测试失败
using test without start nor stop will probably let your tests fail
这篇关于QUnit测试同步Ajax调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文