-
-
Save jaspervalero/78bfdf166b70b48630ab1154a9fca056 to your computer and use it in GitHub Desktop.
Mocking S3 in Node.js using Sinon
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
var Aws = require('aws-sdk'); | |
var sinon = require('sinon'); | |
// Only works for 'createBucket', 'update' and a few others since most API methods are generated dynamically | |
// upon instantiation. Very counterintuitive, thanks Amazon! | |
var createBucket = sinon.stub(Aws.S3.prototype, 'createBucket'); | |
createBucket.yields(null, 'Me create bucket'); | |
// For other methods, we can 'assign' the stubs to the proto, already defined function won't be overridden | |
var listBuckets = Aws.S3.prototype.listBuckets = sinon.stub(); | |
listBuckets.yields(null, 'Me list buckets'); | |
var s3 = new Aws.S3(); | |
s3.createBucket({Bucket: 'my-bucket'}, function(err, arg1) { | |
console.log(arg1); | |
}); | |
s3.listBuckets(function(err, arg1) { | |
console.log(arg1); | |
}); | |
console.log('Called #createBucket() ' + s3.createBucket.callCount + ' time(s)'); | |
console.log('Called #listBuckets() ' + s3.listBuckets.callCount + ' time(s)'); |
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
{ | |
"name": "node-s3-stub", | |
"version": "0.0.0", | |
"description": "", | |
"main": "index.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"author": "", | |
"license": "BSD", | |
"dependencies": { | |
"sinon": "~1.14.1", | |
"aws-sdk": "~2.1.17" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment