Chapter 3 of Hartl’s Ruby on Rails Tutorial is nominally about setting up static pages, but the crux of this chapter was the introduction of Test Driven Development (TDD).
In the last few years there has been major discussion on the necessity of TDD, to the point that David Heinemeier Hansson, the creator of Ruby on Rails chimed in with his own thirty-seven cents! “TDD is Dead,” Hansson says.
I appreciate Hartl’s non-dogmatic approached to this subject by using TDD in moderation. As they say, “everything in moderation is better for you.” Researching and discussing with other senior developers, TDD has the following rules:
- The most important is to write and run the test code before you run the actual application code. Hartl specifically suggest to write the controller and model tests first, and integration tests second. Integration tests are great because this is used to mimic a user dynamically interacting with your web app.
- The first test you run should fail. Then use the errors of that failing test sto help you write the application code.
- Once the code passes the tests, then it can be refactored.
TDD is not a new concept, but Rails attempts to make it little easier from the start. When you generate your new controller, i.e. rails generate controller, Rails automatically creates a test file! In the terminal type:
You should see the controller_test.rb file. Opening that file up in your text editor, you should see generated Ruby code that issues the HTTP operation GET and makes sure it receives a “success” status code.
To write a failing test, Hartl throws in another page inside the Ruby code. Since he built the Home and Help pages, now he wrote a few lines to request an ‘About’ page. After writing that, you run the test by typing in the terminal:
bundle exec rake test
This automatically indicates a fail. Why? Well, because there was no Route that was created to match the About controller. Input a GET request inside the config/routes file and run the test again:
bundle exec rake test
Another fail. Why? Because we didn’t define an ‘About’ action in the Static Pages Controller. Define the about page in the static_pages_controller.rb file. Now, test again– Darn, failed again! What’s the error? There is no View anywhere to be found, i.e. there is no HTML file that Rails can call on to give our users a visible page! Hartl creates an html.erb page by typing:
The other way of creating it is to simply right click on the directory folder and just create a new file with an appropriate name (making sure to have an html.erb extension). Now you can put in some HTML code for a quick presentation, run the test again, and voila, it passed! Which means you can see the ‘About’ page in the browser, along with the ‘Home’ and ‘Help’ pages.
Lastly, code can get a little ugly looking to the human eye, or smelly as it’s called in the tech world, we must refactor. One of the things to look for are repeated code blocks. Employing the DRY (Don’t Repeat Yourself) principle is vital in Rails community and one should be cautious of violating it!
What I took away from this is getting into the skin of Rails and witnessing how the organs operate by utilizing MVC.