Skip to content

Instantly share code, notes, and snippets.

@horike37
Created February 21, 2017 15:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save horike37/39a21b37dbf0770e467c0f1ca0176bef to your computer and use it in GitHub Desktop.
Save horike37/39a21b37dbf0770e467c0f1ca0176bef to your computer and use it in GitHub Desktop.
'use strict';
const AWS = require('aws-sdk');
const BbPromise = require('bluebird');
class AwsProvider {
request(service, method, params) {
const awsService = new AWS[service]();
const req = awsService[method](params);
return new BbPromise((resolve, reject) => {
req.send((errParam, data) => {
const err = errParam;
if (err) {
reject(new Error(err.message, err.statusCode));
} else {
resolve(data);
}
});
});
}
}
module.exports = new AwsProvider();
'use strict';
const aws = require('./awsProvider');
const BbPromise = require('bluebird');
class Index {
constructor() {
this.provider = aws;
}
method() {
return this.provider.request('STS',
'getCallerIdentity',
{}
).then((result) => {
return BbPromise.resolve(result);
});
}
}
module.exports = Index;
'use strict';
const expect = require('chai').expect;
const BbPromise = require('bluebird');
const sinon = require('sinon');
const Index = require('./index');
describe('index', () => {
let index;
beforeEach(() => {
index = new Index();
});
describe('method', () => {
let stsStub;
beforeEach(() => {
stsStub = sinon.stub(index.provider, 'request')
.returns(BbPromise.resolve({}));
});
it('should the correct value', () => {
index.aa().then(() => {
expect(stsStub.calledOnce).to.be.equal(true);
expect(stsStub.calledWithExactly(
'STS',
'getCallerIdentity',
{}
)).to.be.equal(true);
index.provider.request.restore();
});
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment