Chapter 2. Testing saves your bacon
This chapter covers
- Introducing testing approaches
- Test-driven development with MiniTest
- Behavior-driven development with RSpec
Chapter 1 presented an extremely basic layout of a Rails application and an example of using the scaffold generator. One question remains, though: how do you make your Rails applications maintainable?
About the scaffold generator
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. There’s a thread on the rubyonrails-core mailing list where people have discussed the scaffold generator’s downsides: http://mng.bz/g33u.
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 don’t write tests, your alternative is to check the entire application manually every time you make a change, 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) don’t. We’ll do it correctly from step one.[1]
1Unlike certain other books.