Skip to content

Instantly share code, notes, and snippets.

@thehig
Last active July 4, 2016 11:47
Show Gist options
  • Save thehig/fe659f63c9d1e22f3ea28383f31fc8c4 to your computer and use it in GitHub Desktop.
Save thehig/fe659f63c9d1e22f3ea28383f31fc8c4 to your computer and use it in GitHub Desktop.
js: sinon-fixture-wrapper
module.exports = function(url, payload, outputLevel){
// Uncomment to disable the wrapper
// return {
// restore: function(){}
// }
var sinon = require('sinon');
fakeServer = sinon.fakeServer.create();
fakeServer.autoRespond = true;
if(outputLevel === 'once'){
console.log("=== SINON WRAPPER INTERCEPT FOR \n\t"+ url);
console.log("=== Further output is disabled");
}
fakeServer.respondWith("GET", url, function(request){
switch(outputLevel){
case 'short':
console.log("SinonIntercept for " + url);
break;
case 'once':
break;
default:
console.log("=== SINON WRAPPER INTERCEPT FOR "+ url +" ===");
break;
}
request.respond(200, { "Content-Type": "application/json" }, JSON.stringify(payload));
});
// fakeServer.respondWith("GET", url, [200, { "Content-Type": "application/json" }, JSON.stringify(payload)]);
return fakeServer;
}
/* Example Usage:
# Set up a fake server to respond on the given URL with the given object
# Note: Only calls to the provided URL will succeed. ** Everything else will 404 **
fakeServer = undefined
before ->
fakeServer = require('./fixtures/fixture_sinon_wrapper')("http://callbackcatcher.meteorapp.com/search/body.cart_id=573defe0a5af06fc49ddd0b8", require('./fixtures/573defe0a5af06fc49ddd0b8.json'), 'once')
after ->
# Restore the HTTP service afterward so other tests dont break
fakeServer.restore()
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment