Created
July 6, 2019 08:40
-
-
Save azarboon/fdd6ed79df6c1aed15fea1fe8d96faa5 to your computer and use it in GitHub Desktop.
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
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