lamchau
5/16/2015 - 1:06 AM

sinon + ic.ajax

sinon + ic.ajax

function fakeServer(method, url, response) {
  response = response || {
    "data": "default"
  };
  var server = sinon.fakeServer.create(),

    // force a server pass through to enable response delays
    forceServer = sinon.stub(App, "isApiEndpoint").returns(false),

    // ic.ajax will always do an internal lookup before passing it on
    // to $.ajax, so to ensure we are able to bypass without destroying
    // a defined fixture we need to stub it
    bypassLookup = sinon.stub(ic.ajax, "lookupFixture").returns(false);

  server.restore = function() {
    forceServer.restore();
    bypassLookup.restore();
    sinon.fakeServer.restore.call(server);
  };

  server.respondWith(method, url, [200, {
      "Content-Type": "application/json"
    },
    JSON.stringify(response)
  ]);
  server.autoRespond = true;
  return server;
}