And we’re back. In part 1 we installed everything we needed for Tower and generated our app. In part 2 we learned how to run tests, and got our very first pre-generated tests to go from red to green. Now we need to write some tests of our own, then maybe we can go about actually writing some application code.
Tests of Our Own
If you’re returning to this project after taking a break, remember that your going to want to open a couple tabs in the terminal. In the first one, type:
git checkout -b ircbot
This tells git to create a new branch called “ircbot” and switch us into it. When we add a new feature to a project, this is a good idea because if we screw up something badly, we can save all our changes to that branch and then simple delete it, or just leave it there for reference, but switch back to the master branch. We won’t go deeply into that, but again, I recommend checking out the Pro Git book to learn all about using git in your workflow.
Now that we are safely in a new branch run:
This watches for changes to the coffeescript files and compiles them. Then open a second terminal to run your tests with
mocha test/*/*Test.coffee -w
Now let’s get started. Open up the file test/models/ircBotTest.coffee and change “describe ‘relationships’” to “describe ‘methods’”, then add everything I have beneath that here:
The “describe” lines tell mocha that we are creating a new group of tests. “beforeEach” tells mocha, this is code we want you to run before each of these tests. Pretty simple, right? Your tests should still be passing. If something fails, make sure you typed everything correctly. Be careful of identation.
Now for the first test. Add the following beneath the “beforeEach” block, and make sure the identation is even with it.
As soon as you save the file you should get a failing test. What this block of code does is tells sinon-chai to wrap the ircbot (from the beforeEach block )’s ‘init’ method in a special object called a spy. This spy can then return all kinds of useful information to us, like when, how and how many times the method was called during the execution of the code. Very useful for testing. You can read all about sinon’s abilities here. Then, the test saves the ircbot. What we want is for that bot to call an init method as soon as its saved. It doesn’t, yet, but we’ll fix that soon by writing application code. Finally, after checking that, we call “done()” to tell mocha that everything in the test is finished and it can move on. This is very useful, due to the asynchronous nature of coffeescript and node.
Now open up app/models/ircBot.coffee and add the two extra lines you see here:
“@after” is a special command in a tower model that tells Tower to run a method on an instance of that model, after a certain action is taken. Here we tell it that after creating an instance of an ircBot, it should run the init method. Then we write the “init” method. We could have gone ahead and written the init method’s code, but one guideline of BDD is to only write enough code to pass the test. In this case, simply having the init method there is enough. So now what? Well, now we need to write another test telling us what the init method actually needs to do.
How to Train Your Robot (and by “Train” I mean, “Test”)
So, first we need to go into test/server.coffee and add this line right beneath where we required Faker.
This gives us access to node’s “net” module, which we’ll need for testing some basic functionality in our ircBot. Then it’s back to ircBotTest.coffee, and beneath our last test we now should add: