GET
Testing the getPerson function will be a bit simpler than the addPerson function because there is only one stub. We are still going to run two tests (one for if it is there and another for if it isn’t) and it should feel pretty similar to the last one.
You will be stubbing a getFromDb function this time.
Now write a test to see if all the logic works for getting something from the database.
Note that in the req object, the byu_id variable comes from params now and not query. This is because the information comes from the path.
Now you are ready to write the second half of the test! When the data is not there, the get function returns an empty object.
Let’s start by writing “getFromDb”. Put it at the bottom near your other functions.
The get function that Document Client works by passing the TableName and Key. If the information is not there, it returns an empty object. So we will check that by checking its number of items.
The reason you have to format the date that way is because the Enforcer wants a date object instead of a string, which is what DynamoDB gives us.
That was pretty straight forward. Test it out with “npm test”
Run it out in the console, and and then test it with Postman using your API. If your information is there, it should look like this:
Well done!