title: Unit testing AJAX requests with QUnit
body: <p>We are trying to implement QUnit JavaScript tests for a JS-heavy web app. We are struggling to find a way to successfully test methods that involve jQuery AJAX requests. For example, we have the following constructor function (obviously this is a very simplistic example):</p>
  
  <pre><code>var X = function() {
      this.fire = function() {
          $.ajax("someURL.php", {
              data: {
                  userId: "james"
              },
              dataType: "json",
              success: function(data) {
                  //Do stuff
              }
          });
      };
  };
  var myX = new X();
  myX.fire();
  </code></pre>
  
  <p>We are trying to find a way to test the <code>fire</code> method, preferably with a stubbed URL instead of the real <code>someURL.php</code>. </p>
  
  <p>The only obvious solution to me at the moment is add the URL, and the <code>success</code> callback, as arguments to the constructor function. That way, in the test, we can create a new instance of <code>X</code> and pass in the stub URL, and a callback to run when the stub returns a response. For example:</p>
  
  <pre><code>test("Test AJAX function", function() {
      stop();
      var myX = new X();
      //Call the AJAX function, passing in the stub URL and success callback
      myX.fire("stub.php", function(data) {
          console.log(data);
          start();
      });
  });
  </code></pre>
  
  <p>However, this doesn't seem like a very nice solution. Is there a better way?</p>