Test Drive

Day 8 at NSS


Slam the breaks. That’s what test driven development feels like at first. But guess what, if you didn’t stop in time you may have been slammed from the side by some rogue code. Maybe a different analogy is in order: test driven development is to the developer what the shell is to the turtle- it might seem bulky, but it keeps you safe and gives you somewhere to retreat when the world gets overwhelming. And hey, slow and steady wins the race.

So what does the shell look like? Well the first thing you want to do is make sure that we have chai and coffee in our dependencies. Inside your test.js file remember to expect chai.

var expect = require(‘chai’).expect;

Now, figure out what you’re trying to create. Are you trying to create a new object? A prototype function for your object? Test driven development is all about describing what your code will include and what it should do and what you expect it to yield.

Create a new constructor for a Pet object:

a. First describe a 'Pet' function that will be a constructor function

b. Then express what it should do

c. Then we put down what we expect the code to yield

describe('Pet', function(){
  describe('constructor', function(){
    it('should create a new Pet', function(){
      var pet = new Pet();
      expect(pet).to.be.ok;
      expect(pet).to.be.instanceof(Pet);
    });
    it('should create a new Pet with arguments', function(){
      var pet = new Pet('fluffy', 3, 'female', 'lizard');
      expect(pet.name).to.equal('fluffy');
      expect(pet.age).to.equal(3);
      expect(pet.gender).to.equal('female');
      expect(pet.species).to.equal('lizard');
      expect(pet.isZombie).to.be.false;
      expect(pet.wins).to.equal(0);
      expect(pet.health).to.be.within(10, 100);
      expect(pet.energy).to.be.within(10, 100);
      expect(pet.full).to.be.within(10, 100);
    }); 
  });

Then repeat for every prototype function that you want to create!

Of course, to make your tests pass you’ll have to create another file in your app folder that will include your code. The code that corresponds to the test above looks like this:

function Pet(name, age, gender, species) { 
  this.name = name;
  this.age = age;
  this.gender =gender;
  this.species = species;
  this.isZombie = false;
  this.wins = 0;
  this.health = Math.floor(Math.random() * 91) +10;
  this.energy = Math.floor(Math.random() * 91) +10;
  this.full = Math.floor(Math.random() * 91) +10;
}

It creates the object and constructor that we previously described, and it does what we said the constructor to do, and, best of all, it yields what we expected it to yield!

For a more in-depth look at how a test and its corresponding code can be organized across multiple files check out the project we created in class where we have quirky creatures attacking each other (this is not a cat video). Note, you can make test-driven development a lot more entertaining if you use variable names, or create objects with names that make you laugh when you’re trying to explain your code/ask someone about their code.