Testing in Rails

Rails extends the Test::Unit framework to include new assertion methods that are specific to web applications and to the Rails framework. Rails also provides explicit higher-level support for testing by including a consistent method for loading test data and a mechanism for running different types of tests.

Unit Tests, Functional Tests, and Integration Tests

You’ve already seen that Rails generates unit and functional tests. You’ve also noticed us mention integration tests. We’re going to walk you through some basic tests of each type. Look at your Photo Share application’s directory tree, and you’ll find that it contains a test subdirectory. All tests reside under this test subdirectory, which has several subdirectories of its own:

unit

Holds all unit tests

functional

Holds all functional tests

integration

Holds all integration tests

fixtures

Contains sample data for all tests

In fact, you’ve already been using test fixtures to build common, repeatable test data that you’ll use to take your application through a test drive. Take a look at test/unit, and you’ll see that it already contains category_test.rb, photo_test.rb, slide_test.rb, and slideshow_test.rb. They are test case skeletons created by Rails when we generated our model classes. But before you can start filling out these skeleton test files, you first need to understand Rails’ environments and fixtures.

Environments

We software developers have always distinguished between code running in some form of development ...

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.