Chapter 7. Testing

You probably always write automated tests for all your software ... or feel guilty for skipping them. Dynamically typed languages such as Ruby don’t have a compile step that can catch errors, as Java or C++ does. Well, you’ll be happy to know that Ruby on Rails makes automated testing very easy. In Rails, testing is not something that was bolted on afterwards. Testing has been built in from the very beginning. Rails was designed to be testable and to produce applications that are testable. It is so easy to create automated tests for a Rails appli187cation that you should feel guilty if you don’t!

We’ve come this far with our Photo Share web application, but we haven’t yet created any tests. In truth, this was deliberate. You had enough new things to learn as it was. But now it is time to rectify that oversight and start adding tests to our application. So let’s skip the guilt and code some tests.

Background

Instead of wasting time running through the litany of reasons why automated testing is probably the single most important thing you can do to increase the quality and reliability of your software, we’ll get right down to it. If you haven’t gotten the testing religion yet, just Google for “benefits automated testing.”

Rails encourages you to create a well-tested application by actively generating default test cases and setting up scripts and tools to run three different kinds of tests. For example, when you use script/generate to create your models and controllers, ...

Get Ruby on Rails: Up and Running now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.