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)