Created
April 30, 2012 08:52
-
-
Save kimjoar/2556684 to your computer and use it in GitHub Desktop.
Wrapping Sinon.js Ajax mocking
This file contains hidden or 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
| 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