1

I'm doing a very simple xhr mock example and need the ability to setup the return success (also in this specific example I can't use a library).

  function fakeHttpRequest(json) {                                                      
    $.ajaxSetup({
      complete: function() {
        this.success.call(this, json);
      }
    });
  }

What I'm doing above works great except that if I call it 3 times, the last integration test I'm using this in returns 3 different times (instead of the 1 as I expected). Is it possible to invoke this and clear it or reset it between tests?

Update

Here is what I ended up with

function fakeHttpRequest(json) {
   jQuery.ajax = function(opts) {
     opts.success(json);
   }
}
Toran Billups
  • 27,111
  • 40
  • 155
  • 268

1 Answers1

0

If you only need to fake ajax success response, you can always fake it (see this question):

var fakeResponse;

$.ajax = function( options ) {
  options.success( fakeResponse );
}

You only need to set the fakeResponse before having an object call $.ajax and it will call the object's success function.

Community
  • 1
  • 1
mor
  • 2,313
  • 18
  • 28
  • Even after putting the $.ajaxSetup() in my setup or teardown (QUnit) I see multiple logs when a single test runs and the production code hits $.getJSON directly. (shown above in my question) – Toran Billups Jul 20 '13 at 20:32
  • is there a better way to achieve this? I simply need to fake the success of an xhr when doing $.getJSON in production code (and something that can safely be reset between tests) ? – Toran Billups Jul 20 '13 at 20:40