没有得到Mockjax和JsTestDriver的回复

时间:2011-07-06 15:13:44

标签: javascript jquery js-test-driver

我正在尝试使用mockjax来模拟我的jQuery Ajax调用;但是,我无法找到有效测试答案的任何好的,有效的例子。

以下代码是jQuery.Ajax对象的简单包装器。它有效,但我想写一些测试:

Ajax Wrapper

if (!namespace){ 
    var namespace = {};
}


namespace.Ajax = (function($){

    function defaultError(xhr, err, msg){
        if (typeof console !== 'undefined'){
            var log = console.error;

            if (!log) {
                log = console.log;
            }

            log("==========================================");
            log("An error occurred with the request."); 
            log("- Status Code: " + xhr.status);
            log("- Status Text: " + xhr.statusText);
            log("- Response Text: " + xhr.responseText);
            log("- Message: " + msg);
            log("==========================================");
        }
    }

    function getData(url, data, successCallback, errorCallback){
        if (!errorCallback){
            errorCallback = defaultError;
        }

        $.ajax({
            url: url
            , data: data
            , success: successCallback
            , error: errorCallback
        });
    }

    function postData(url, data, successCallback, errorCallback){
        if (!errorCallback){
            errorCallback = defaultLog;
        }

        $.ajax({
            type: "POST"
            , url: url
            , data: data
            , success: successCallback
            , error: errorCallback
        });
    }

    return {
        getData : getData
        , postData: postData
    }
}(jQuery));

我对getData进行了以下JsTestDriver测试。

测试类

TestCase("Ajax Object Test Fixture", {
    "test Calling getData Should Return content" : function(){
        var results;
        var obj = namespace.Ajax;

        $.mockjax({
            url: "/test"
            , responseTime: 1
            , responseText: "success"
        });


        obj.getData("/test"
                    , null
                    , function(data){results = data; });

        setTimeout(function(){assertEquals("'success' Should be Returned.", "success1", results);}, 500);
    }
});

assertEquals函数应该在此示例中返回false ,因为我期待“success1”,但在整个代码中我尝试将值设置为“success”。我希望这个测试失败所以我知道它正在运行。尽管如此,测试成功了。我有一个事件尝试将successCallback函数设置为results = "success"并且它仍然没有“失败”测试。

如何设置此测试以确保返回模拟响应,以便我没有得到我目前的误报?

1 个答案:

答案 0 :(得分:1)

我认为这里的问题是setTimeout导致测试用例退出而不运行任何断言,并且assertEquals在一个单独的“线程”中被调用。

这里需要的是异步测试用例:

(function(namespace){
var testcase = AsyncTestCase('AjaxObjectTest');

/**
 * Each asynchronous testcase method receives a queue object.
 */
testcase.prototype.testGetData = function(queue) {
    var results;
    var obj = namespace.Ajax;

    $.mockjax({
        url: "/test",
        responseTime: 1,
        responseText: "success"});

    // Here we pass a function to the queue call method, which will
    // pause the testcase execution until all callback methods have been
    // called, or until it expires.
    queue.call('Expecting a callback', function(callbacks){

        // The callbacks object is a factory for creating required 
        // callback functions. This queued function will wait until it
        // has been called. You can specify the number of times you require
        // it to be called as the second parameter of the add method.
        var handler = callbacks.add(function(data){
            assertEquals("'success' Should be Returned.", "success1", data);
        });

        obj.getData("/test", null, handler);
    }
});
})(namespace);

您可以在单个测试中拥有任意数量的队列调用,并且只有在前一个完成后才会运行所有队列调用。您可以在JsTestDriver项目wiki中阅读有关异步测试用例的更多信息。希望有所帮助!

哦,差点忘了!为避免在没有断言的情况下通过测试,可以使用expectAsserts函数:

testcase.prototype.testSomeStuff = function(){
    expectAsserts(2);

    // Test your stuff here
};