Skip to content

Instantly share code, notes, and snippets.

@kimjoar
Created April 30, 2012 08:52
Show Gist options
  • Select an option

  • Save kimjoar/2556684 to your computer and use it in GitHub Desktop.

Select an option

Save kimjoar/2556684 to your computer and use it in GitHub Desktop.
Wrapping Sinon.js Ajax mocking
function fakeResponse(response, options, callback) {
var statusCode, headers, server, resp;
// some default values, so we don't have to set status code and
// content type all the time.
statusCode = options.statusCode || 200;
headers = options.headers || { "Content-Type": "application/json" }
// we create what Sinon.js calls a fake server. This is basically just
// a name for mocking out all XMLHttpRequests. (There are no actual
// servers involved.)
server = sinon.fakeServer.create();
// we tell Sinon.js what we want to respond with
server.respondWith([statusCode, headers, response]);
callback();
// this actually makes Sinon.js respond to the Ajax request. As we can
// choose when to respond to a request, it is for example possible to
// test that spinners start and stop, that we handle timeouts
// properly, and so on.
server.respond();
server.restore();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment