And we’re back with our tower.js tutorial. We are building an app using tower.js, BDD techniques, on node.js. If you are just finding the series, begin with part 1 so that you aren’t completely lost. When we left off, we had just merged the code for our ircBot, along with its tests, into our main git branch. We are going to be adding some more features, so let’s checkout a new branch for that. This way, if anything goes horribly awry, we can easily revert those changes and be back to a working version of our app in an instance by giving git a few easy instructions.
git checkout -b messages-and-gists
Now, let’s generate some scaffolds, alright?
First, in your terminal, we’re going to give tower a couple of commands. Since these two models will actually have a relationship to each other, we are going to generate them both at once.
tower generate scaffold Message user:string body:string belongsTo:gist
tower generate scaffold Gist text:object gitHubUrl:string hasMany:messages
In your terminal, you should see a lot of output, like so:
Next we need to get tower watching our source files again, and restart our tests. First start the watcher, with:
Next our tests:
mocha test/*/*Test.coffee -w
You’ll quickly see our tests are no longer passing. Let’s do something about that. Open test/controllers/gistsControllerTest.coffee and test/controllers/messagesControllerTest.coffee and add this line towards the top, beneath where it says “url = undefined”.
If you got the indentation correct, those tests should pass and it should now be complaining about something else. Next we need to define our factories. In test/server.coffee, add these lines beneath where we added our previous factory:
If your tests stop running with an error at any point, just restart them after the next step. In both controller tests, messagesControllerTest.coffee and gistsControllerTest.coffee, under “describe ‘index’”, change @headers to request.headers and “Content-Type” to “content-type”, like this:
Again, for both sets of controller tests, in the routes section, for both the “show” and “edit” action tests, change lines calling “new” to create instances of the models to “build” and “save”:
gist = new App.Gist(id: 1)
message = new App.Message(id: 1)
Finally, down beneath the routes test, where it says “describe ‘index’” in the “beforeEach” section beneath “message = record”, we need to save that message (and do the same thing for the gist in the gistsControllerTest). Like so:
With those changes, you tests should now look like mine. First messagesControllerTest.coffee:
Now, a couple quick changes to the models tests. Open test/models/messageTest.coffee and test/models/gistTest.coffee and in the “beforeEach” change “new” to build again, like so:
If you’ve done that for both model tests, gists and messages, then all your tests should now be green and passing. Phew. That was a lot of work just to fix failing tests we didn’t even write. But, the good news is, we now have a suite of tests on our scaffolds testing basic functionality, and we didn’t even have to write them. In the future, tower will generate these correctly and we won’t have to go to so much trouble adjusting them. Perhaps even by the time you read this.
Come back for Part Five, where we’ll write more tests of our own, and teach our ircbot how to save messages, and create gists.