QUnit testing synchronous ajax calls

只谈情不闲聊 提交于 2019-12-25 03:22:45

问题


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().

Consider this:

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
};
} ();

How can we write test cases for Class.Students() method when the 'async' property is 'true' and 'false' ?


回答1:


with asyncTest and asnyc true or false:

asyncTest("test", function () {
    init.Students(someBranch, function (a, b, c) {
        ok(true); //add your tests here
        start();
    });
});

with test and stop/start:

   test("test", function () {
        stop();
        init.Students(someBranch, function (a, b, c) {
            ok(true); //add your tests here
            start();
        });
    });

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



来源:https://stackoverflow.com/questions/12833913/qunit-testing-synchronous-ajax-calls

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!