I just discovered the mockjax library, and it seems almost perfect for my testing needs. I say "almost", because there's one thing I can't seem to simulate: $.ajaxPrefilter
handlers.
Normally in jQuery you can specify:
$.ajaxPrefilter(function() {
doSomethingOnEveryAjaxCall();
});
and then whenever anyone uses $.ajax
(or $.get
, $.post
, etc.) that doSomethingOnEveryAjaxCall
function will be called.
However, if I do:
$.mockjax({url: '/test', responseText: 'test'});
$.ajax({url: '/test'});
the prefilter I setup (doSomethingOnEveryAjaxCall
) doesn't get called.
Is there any way to modify that code so that the prefilter gets called even on mockjax requests/responses?
It turns out mockjax was capable of triggering ajax prefilters ... I just forgot to supply all of the necessary headers to make my prefilter actually trigger. Once I tried using the proper headers, the prefilter triggered as expected!