Last active
August 29, 2015 13:56
-
-
Save th3james/8829607 to your computer and use it in GitHub Desktop.
Sinon fake server example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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