Chapter 7. Testing

These days, you probably always write automated tests for all your code...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, so Rails makes automated testing a priority. In Rails, testing is not some feature based on a buzzword that was bolted on afterward. 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 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. Typically, you’ll create tests as you go. In truth, skipping the tests was deliberate. We decided to introduce core Rails concepts first without muddying the waters with tests. But now it is time to rectify that situation and start adding tests to our application. So, let’s skip the guilt and code some tests.

Background

Rails encourages you to create a well-tested application by actively generating default test cases, fixtures, and helpers for three different types of tests: unit tests for models, functional tests for your controllers, and integration tests for your whole application. Each time you use script/generate to create your model, Rails creates a skeleton unit test, fixture, and helper. When you generate a controller, Rails creates a functional test.

Keep in mind that Rails uses an environment ...

Get Rails: Up and Running, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.