Last active
November 16, 2017 10:26
-
-
Save wryun/347283d8e715afc4f904ffe5838c536c to your computer and use it in GitHub Desktop.
Mocking js aws-sdk using sinon (rough notes)
This file contains 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
beforeEach(function () { | |
this.aws = {}; | |
stubAwsRequest(stubs); | |
}); | |
afterEach(function () { | |
AWS.Request.prototype.runTo.restore(); | |
}); | |
AWS.Request.prototype.originalRunTo = AWS.Request.prototype.runTo; | |
function stubAwsRequest (stubs) { | |
sinon.stub(AWS.Request.prototype, 'runTo', function (state, done) { | |
// We take a nasty shortcut here, and assume that any defined | |
// state doesn't get to actually sending (true as of now | |
// in 2.5.2 - this is only used for 'build' internally). | |
if (state !== undefined) { | |
return this.originalRunTo(state, done); | |
} | |
// undefined state means we want to run to the end... | |
// i.e. stop at 'send' and then emit. | |
return this.originalRunTo('send', function (err) { | |
if (done) { | |
done.apply(this, arguments); | |
} | |
// if there's an err, it will already have triggered | |
// complete/error etc. | |
if (!err) { | |
try { | |
this.response.data = stubs[this.service.serviceIdentifier][this.operation](this.params); | |
this.emit('success', [this.response]); | |
} catch (err) { | |
this.response.error = err; | |
this.emit('error', [this.response]); | |
} | |
this.emit('complete', [this.response]); | |
} | |
}); | |
}) | |
} | |
define('some test using sns', function () { | |
beforeEach(function () { | |
this.aws.sns = { | |
publish: sinon.stub() | |
} | |
}); | |
it('do something', function () { | |
dostuff(); | |
this.aws.sns.publish.should.have.been.calledWith({...}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment