I'm testing my frontend code using qunit and mockjax. The structure of AJAX tests in mockjax's own test code looks like this (jsfiddle):
var testURL = "/test/data",
testData = { a: 1, b: "c" };
asyncTest("AJAX response test", 1, function() {
$.mockjax({
url: testURL,
responseText : JSON.stringify(testData)
});
$.ajax({
url: testURL,
dataType: "json",
success: function(data) {
deepEqual(data, testData, 'AJAX response is OK');
},
complete: function() {
start();
}
});
$.mockjaxClear();
});
According to the mockjax documentation:
* $.mockjaxClear()
Removes all mockjax handlers.
What I don't understand is why mockjaxClear
is called right after the $.ajax()
call. The problem is if it does some sort of cleanup, as the documentation says, this cleanup will run before the AJAX response arrives (pls. see the console of this jsfiddle). It seems more logical for me to do the cleanup in the handler of the complete
event. Can anyone explain me why it is better to call mockjaxClear after $.ajax()
?