Skip to content

Instantly share code, notes, and snippets.

@th3james
Last active August 29, 2015 13:56
Show Gist options
  • Save th3james/8829607 to your computer and use it in GitHub Desktop.
Save th3james/8829607 to your computer and use it in GitHub Desktop.
Sinon fake server example
test(".save persists the model to the server and adds the ID to the attributes", function() {
var page, persistenceId, server;
// Create a fake server to block AJAX requests
server = sinon.fakeServer.create();
// Upon receiving a request to '/pages', respond with an ID
persistenceId = 6;
server.respondWith(new RegExp("/pages"), JSON.stringify({
id: persistenceId
}));
page = new Page();
page.save();
// Respond to the pending requests
server.respond();
// We want to restore the server even if the assertions fail, so we use a finally block
try {
assert.strictEqual(page.id, persistenceId, "Expected the page to have the ID set from the server response");
} finally {
server.restore();
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment