BYU Web Service Manual
HomeToolsWeb Service manual Edit Page

POST

It is always good practice to start testing first.

We are going to need to be in our add-person-test file.

Let’s set things up by bringing in the modules we will need.

Mocha gives us a function called describe. This allows us to describe what we are testing.

Our addPerson Function is going to call the Persons API and put information into our DynamoDB. We don’t want it to actually do that, so we are going to build “stubs” using sinon.

Stubs will take the place of the functions when they are run. The “beforeEach” and “afterEach” functions will build and restore the stubs between each function. This also means that when we implement our code, we will need to make a “callPersonApi” function and a “putToDb” function.

Let’s write a test inside of our test. It is done the same way, and we will also describe what it should do using the it function from mocha.

We should start by telling it what should expect from the persons API. We don’t have to tell it everything; that would be a lot of data. We should tell it only what is pertinent to us (if our program is looking for anything else we will know there is a problem). The “putToDb” function won’t actually return anything, so we can tell the stub to pass an empty object.

We are testing the “addPerson” which takes a req object and a res object. Let’s start by making the req object. This comes from your query parameters, so let’s make it look like that.

The res object is going to receive data from the express module, so it is going to have an object called “send” and “sendStatus”.

Things are going to be called to “send” and “sendStatus,” so we are going to want to make them stubs so we can test them.

We want to test the data that actually gets called to “send,” so we are going to make a fake response using sinon’s “callsFake” function. Then we will use “expect” from chai to see if everything matches up.

All that’s left to do is to run the function and then make sure all the stubs got called the right amount of times. If they don’t, then we have a problem. For example, it would be bad if things got put to DynamoDB multiple times or if we put a lot of unnecessary stress on the persons API.

Let’s also test that if a BYU_ID is sent that doesn’t exist, it will return a 404.

It follows pretty much the same pattern, but now we care about the “sendStatus” object.

You can run the test in the terminal with “npm test” (or “npm t” for short). It won’t work right now because you have no code. But we will fix that in a second!

Right now, your favoritecolor.js file should look a bit like this

(make sure that you change your const params to a let at line 9):

We are going to need to export our function so that our tests and the enforcer can use it. We are also going to make it an async function so that it is easy to manage all of our calls.

Now you can update the addPerson function. The Document Client takes an object that has a TableName(where you are going to put it) and Item(what you are going to put) .

It is good practice to use a try catch block in an async function.

First, we want to call the persons API. But to do that we need to first make a function like we planned to stub during testing.

While we are here, let’s make a putToDb function. You may wonder why it is “putToDb” and not “postToDb”. Document Client has a put, delete, and get function but no post function. The post function creates a new object if there is not already an object with that key (in this case byu_id) in the database, so it will work for us.

Let’s call the persons API and check if the BYU ID that is sent exists.

Now we just to take the information and return it.

Before we run our test, make sure that you go to your package.json file and add the mocha test to allow tests to run when you say nmp test or npm t.

Let’s run our test! (You may need to run awslogin first)

If it worked, that’s great!!

Let’s run it in the terminal (remember to log into aws cli if you haven’t already) and test it using Postman using your own BYU ID.

After it works in Postman, you can see if it posted to DynamoDB.

That looks great! (I even tried a few other names)

Contributing Source Issue Tracker