Skip to content

Instantly share code, notes, and snippets.

@jonasporto
Created September 22, 2016 17:10
Show Gist options
  • Save jonasporto/c3dd601c13d411739fcb96bfb22f8995 to your computer and use it in GitHub Desktop.
Save jonasporto/c3dd601c13d411739fcb96bfb22f8995 to your computer and use it in GitHub Desktop.
example of stub with sinon
// install sinon via npm
// npm install sinon --save-dev
// require sinon in your test/client_api.js
var sinon = require("sinon");
// add this test expectation
it("cover error", function (done) {
var url = URL_ROOT + "/client";
// stub client, changing return of Client.find
stubClient = sinon.stub(Client, 'find', function(obj, callback) {
callback(new Error('An Error Has Occurred'), []);
});
superagent.get(url)
.set("Authorization", token)
.end(function (error, res) {
// make sure of call restore to avoid, unexpected result for others tests expectation
stubClient.restore();
assert.equal(res.status, status.INTERNAL_SERVER_ERROR);
done();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment