Skip to content

Instantly share code, notes, and snippets.

@lamchau
Created May 16, 2015 01:06
Show Gist options
  • Save lamchau/554e5f53e52ff7defabe to your computer and use it in GitHub Desktop.
Save lamchau/554e5f53e52ff7defabe to your computer and use it in GitHub Desktop.
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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment