amazon s3 - node.js stubbing AWS S3 method in request spec with sinon -
i've got express
based app running on node.js 0.12.2
uses s3.headbucket
method aws-sdk 2.1.22
return json response depending upon whether particular bucket exists or not.
i've been struggling directly stub out call s3.headbucket
sinon. i've managed work around creating s3wrapper module requires aws-sdk
, instantiates , returns s3
variable, however, i'm sure can done without using wrapper module , can instead stubbed directly sinon, can point me in right direction?
below working code (with wrapper module s3wrapper.js
i'd remove , handle stubbing in status_router_spec.js
file). in other words, i'd able call s3.headbucket({bucket: 'whatever' ...
instead of s3wrapper.headbucket({bucket: ' ...
, able stub out s3.headbucket
call own response.
status_router_spec.js
var chai = require('chai'), sinon = require('sinon'), request = require('request'), myhelper = require('../request_helper') var expect = chai.expect var s3wrapper = require('../../helpers/s3wrapper') describe('my router', function () { describe('checking service status', function () { var headbucketstub beforeeach(function () { headbucketstub = sinon.stub(s3wrapper, 'headbucket') }) aftereach(function () { s3wrapper.headbucket.restore() }) describe('when no errors returned', function () { it('returns healthy response', function (done) { // pass null represent no errors headbucketstub.yields(null) request.get(myhelper.appurl('/status'), function (err, resp, body) { if (err) { done(err) } expect(json.parse(body)).to.deep.eql({ healthy: true, message: 'success' }) done() }) }) }) }) })
s3wrapper.js
var aws = require('aws-sdk') var s3 = new aws.s3() module.exports = s3
status_router.js
var router = require('express').router var s3wrapper = require('../helpers/s3wrapper.js') var router = new router() function statushandler (req, res) { s3wrapper.headbucket({bucket: 'some-bucket-id'}, function (err) { if (err) { return res.json({ healthy: false, message: err }) } else { return res.json({ healthy: true, message: 'success' }) } }) } router.get(/^\/status\/?$/, statushandler) module.exports = router
Comments
Post a Comment