Skip to content

Instantly share code, notes, and snippets.

@azarboon
Created July 6, 2019 08:40
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 azarboon/fdd6ed79df6c1aed15fea1fe8d96faa5 to your computer and use it in GitHub Desktop.
Save azarboon/fdd6ed79df6c1aed15fea1fe8d96faa5 to your computer and use it in GitHub Desktop.
const AWS = require('aws-sdk');
const test = require('ava');
const sinon = require('sinon');
const App = require('../../app.js');
const lib = require('../../lib.js');
const dynamoDb = new AWS.DynamoDB.DocumentClient();
const sandbox = sinon.createSandbox();
let event, context, stub;
test.after('restore stubbed functions to original', async t => {
stub = sandbox.restore();
});
test('test readItem by stubbing', async t => {
//making DynamoDb's get method to return our specified item
stub = sandbox.stub(dynamoDb, 'get');
stub.callsFake(() => ({
promise() {
return Promise.resolve({
item: {
key1: "value1"
}
});
},
}));
//injecting stubbed dynamoDb into our business logic
const app = new App(dynamoDb);
try {
let result = await app.readItem("one");
t.is(result.statusCode, 200)
t.is(JSON.parse(result.body).message.item.key1, "value1")
} catch (e) {
t.fail(e)
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment