Skip to content

Instantly share code, notes, and snippets.

@mrogach2350
Created September 30, 2016 19:15
Show Gist options
  • Save mrogach2350/c3b45d3535c74739bd8871e490cf0f1b to your computer and use it in GitHub Desktop.
Save mrogach2350/c3b45d3535c74739bd8871e490cf0f1b to your computer and use it in GitHub Desktop.

#TDD, RSPEC, and You!

##What is TDD?

Test Driven Development a.k.a. TDD is based around writing tests and then developing your code to satisfy those tests. This can done in JavaScript and Ruby utilizing Mocha and RSPEC, respectively.

Let's explain TDD in a metaphor. Your app is a cafe and your test is a food critic. As an ambitious cafe owner, you want to make sure your cafe can satisfy a picky critic before ever opening it to the public, ensuring a great experience for customers before they even set foot in your fine establishment. The critic has to be able to critique specific menu items. Could you imagine a real food critic come up to the counter and say, "Gimme anything off the menu. I'll review it." The barista behind the counter wouldn't know what to give the critic. The same would happen if the critic walked in and ordered Lobster Wellington, an item that is not offered by the cafe. The barista would again be bemused and unable to satisfy the critic. As such, when we write tests, we need to be able to review each item the cafe can offer, as well as train the barista in what to do when he asked for food beyond his means.


A quick reference guide for this metaphor:

Critic = Test

Barista = Controller

Cafe = App

Menu = Resources in DB


  1. Challenges of TDD

    • High Organizational Demand: TDD requires a largely complete picture of functionality and end-goals. Prior to even touching the test or app code, Devs must know what they want at the end to write good tests. You need to write your cafe blueprint before you can tell the critic what to expect and long before your ever break ground on the acutally cafe.
    • Demands Details: As mentioned above. You need to be able to prime the critic in what is offered on the menu. Additionally, you need to train your barista in what to do when he/she is presented with requests outside their ability. As it applies to code, you need to have routes, resources, and methods fleshed out in advance to prime your tests with the appropriate terminology. It's similar to size naming in popular coffee spots. While Venti and Large may mean the same thing, it can make a difference when you order to ensure you recieve the size you acutally want.
  2. Advantages of TDD

    • Results Driven: The beauty of TDD comes in the way that you as the Dev are able to recieve concise and specific feedback from your tests as you work through the challenge of actually building your app. For all the work you need to put into developing your tests, you will be rewarded proportionally in how much time you save fixing bugs later. While this may not be worth it if your dream was to serve coffee from a hand cart on the street, but is certainly worth the effort when you want open a full-service cafe in a high-traffic part of town.
    • Keeps the Team on track: This point is the inverse of the challenge of detail-oriented testing. When you work with collaborators, written tests will ensure each member of the team is consistent across the board in their terminology and overall vision of the project.

    For the purpose of this Gist, we will be focusing on using RSPEC with Rails.

    For More Details: Rails RSPEC docs


    If you are working in JS and would like to pursue TDD, please visit the following link.

    For More Details: Mocha JS

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment