Chapter 2. Testing saves your bacon

 

This chapter covers

  • Writing tests with RSpec and Cucumber
  • Maintaining code through tests
  • Test- and behavior-driven development

Chapter 1 presented an extremely basic layout of a Rails application and an example of the scaffold generator.[1] One question remains, though: how do you make your Rails applications maintainable?

1 We won’t use the scaffold generator for the rest of the book because people tend to use it as a crutch, and it generates extraneous code.

The answer is that you write automated tests for the application as you develop it, and you write these all the time.

By writing automated tests for your application, you can quickly ensure that your application is working as intended. If you didn’t write tests, your alternative would be to check the entire application manually, which is time consuming and error prone. Automated testing saves you a ton of time in the long run and leads to fewer bugs. Humans make mistakes; programs (if coded correctly) do not. We’re going to be doing it right from step one.[2]

2 Unlike certain other books.

2.1. Test- and behavior-driven development

In the Ruby world a huge emphasis is placed on testing, specifically on test-driven development (TDD) and behavior-driven development (BDD). This chapter covers three testing tools, Test::Unit, RSpec, and Cucumber, in a basic fashion so you can quickly learn their format.

2.2. Test-driven development

2.3. Behavior-driven development

2.4. Summary