Day 26 (week 6) — TDD

Eric Abell
Aug 22, 2017 · 4 min read

TDD — test driven development. The idea is that as you develop software, a good practice is to develop a requirements document first, then write tests that exercise those requirements, and finally you write the actual code to implement the requirements.

I’ve seen this before when writing test code for Motorola. I’ve also tried to incorporate this idea when I taught Python programming to students. It can be extremely satisfying to go from all tests failing to all tests passing.

A number of useful things happen when you have good tests in place. First, you can run the test suite and know instantly if your code is doing what it is supposed to. Second, if you make modification to the implementation and some tests begin to fail, you have a much better idea of where to look for the bug. Third, if users find bugs in your program you can add the required test cases to eliminate those bugs. Fourth, if your code interacts with other people’s code and something is broken it can be satisfying to say with certainty that your code is tested. Is theirs?

We spent a little time looking at Jest and supertest. Both of those are frameworks for writing tests in Javascript. I would have liked to see more of this, but I’m fine with reading up on the documentation and writing my own tests as we move forward.

We also discussed the Model-View-Controller design pattern. In this pattern, you separate the code of your app into three distinct categories. Code that handles your data (perhaps a database or a file) is done in the model. Code that handles how your data is displayed to the user is done in the view. Code that handles the business logic of how to process the data is done in the controller. For our purposes in Express, routes will handle most of the controller logic.

We had two activities in the morning. The first was to refactor our user directory project from a week ago into a Model-View-Controller structure. This was pretty easy, as it just meant that we moved the data that was previously in a file data.js into a file that used module.exports and then require'd that file in our app.js file. In addition to doing the model refactoring, I also added an Express Router and put in an additional route to fetch all the users in the directory that were employed. The code to do that:

router.get('/employed', (req, res) => {
// get only the employed folks
let employedUsers = data.users.filter( (user) => {
console.log(user);
if( user.job === null ) {
// they are unemployed
return false;
}
return true;
});
res.render('directory', {users: employedUsers});
})

The second activity was to implement a Caesar Cipher in Javascript. We were provided with the test code that tests both the encryption path and the decryption path. Since I’ve done this many times in the past with my Advanced Calculus students, this was a piece of cake. Basically, the idea is to convert all the characters in the string to their ASCII code equivalent, add an offset to that code corresponding to the Caesar Cipher algorithm, use modular arithmetic to make sure that we wrap around from z to a instead of just going off to other characters besides a-z. Last, convert the ASCII code back into a letter and join the letters to form the encrypted string.

You can look at my implementation here.

After lunch, I worked on my weekly project of a word-guessing game. Things are coming along quite nicely. I’m done with all the basic functionality of the game and have refactored my code twice because I have figured out better ways of doing things. :-)

Right now, I’m working on receiving keyboard press events to handle the user’s guess rather than just using a simple form on the page. All I have to figure out is how to get the client-side JS code to send the properly-formatted form data to my Express server and BAM! that will be a nice feature of the app.

Tomorrow we start MongoDB in earnest. I love using Mongo, and I’m looking forward to doing more cool stuff with it!

)
Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade