QUnit测试同步ajax调用

时间:2012-10-11 06:59:05

标签: javascript jquery qunit

有人可以告诉我是否需要使用asyncTest()来测试同步ajax调用,或者test()可以在没有start()和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()方法编写测试用例?

1 个答案:

答案 0 :(得分:0)

使用asyncTest和asnyc为true或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处理异步

使用不启动或停止的测试可能会让您的测试失败