Skip to content

Instantly share code, notes, and snippets.

@mattcollier
Last active August 29, 2015 14:25
Show Gist options
  • Save mattcollier/6e2d29ee44bbf9fc2b21 to your computer and use it in GitHub Desktop.
Save mattcollier/6e2d29ee44bbf9fc2b21 to your computer and use it in GitHub Desktop.
Correct sinon usage?
'use strict';
var sinon = require('sinon');
var request = require('request');
describe('standalone-issuer', function() {
var rPost;
beforeEach(function(done) {
rPost = sinon.stub(request, 'post');
rPost.withArgs({uri:'somewhere',body:'something'}, sinon.match.any)
.yields(null, {statusCode: 200}, JSON.stringify({someData: 'something!'}));
rPost.withArgs({uri:'somewhere',body:'somethingElse'}, sinon.match.any)
.yields(null, {statusCode: 400}, JSON.stringify({someData: 'something!'}));
done();
});
afterEach(function(done) {
request.post.restore();
done();
});
it('should return 200', function(done) {
request.post({uri:'somewhere',body:'something'}, function(err, res, body) {
should.not.exist(err);
res.statusCode.should.equal(200);
sinon.assert.calledWith(rPost, {uri:'somewhere',body:'something'}, sinon.match.any);
done();
})
});
it('should return 400', function(done) {
request.post({uri:'somewhere',body:'somethingElse'}, function(err, res, body) {
should.not.exist(err);
res.statusCode.should.equal(400);
sinon.assert.calledWith(rPost, {uri:'somewhere',body:'somethingElse'}, sinon.match.any);
done();
})
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment