How to fake jquery.ajax() response?

前端 未结 6 510
孤独总比滥情好
孤独总比滥情好 2020-12-07 20:56

I am writing some QUnit tests for a JavaScript that makes AJAX calls.

For isolation I overwrite $.ajax to write the parameter array of an AJAX call to a

6条回答
  •  猫巷女王i
    2020-12-07 21:03

    After reading inspired by @Robusto and @Val, I found a method that works:

    //Mock ajax function
    $.ajax = function (param) {
        _mockAjaxOptions = param;
        //call success handler
        param.complete("data", "textStatus", "jqXHR");
    };
    

    Instead of raising the event from any real $.ajax code or by triggering any events, I have my fake ajax object call the function (which is passed in as a parameter to $.ajax()) as part of my fake function.

提交回复
热议问题