javascript - How to stub aws-sdk -
lets have following
// file sample.js var aws = require('aws-sdk'); var dynamodb = new aws.dynamodb(); exports.processdata = function(){ var data = dynamodb.getitem(params); // data };
how can write unit tests above code sample.
//file sample_test.js var aws = require('aws-sdk'); var sinon = require('sinon'); // following code doesnt seem stub function // actual function still used in sample.js var getitemstub = sinon.stub(); aws.dynamodb.prototype.getitem = getitemstub; var sample = require('./sample');
what way stub aws-sdk api. thinking of using sinonjs achieve it, open other libraries , suggestions .
we have created aws-sdk-mock npm module mocks out aws sdk services , methods. https://github.com/dwyl/aws-sdk-mock
it's easy use. call aws.mock service, method , stub function.
aws.mock('dynamodb', 'getitem', function(params, callback) { callback(null, 'success'); });
then restore methods after tests calling:
aws.restore('dynamodb', 'getitem');
or restore them call:
aws.restore();
Comments
Post a Comment