1.7. Testing Rails

Testing has become vital to building high-quality software that is to stand the test of time. With proper testing, you can minimize rewriting code and avoid spending time debugging it. This documentation is adapted and abbreviated from the Ruby on Rails online manual, chapters 21–28 (http://manuals.rubyonrails.com/read/chapter/21).


According to a poll done on RailsEnvy.com, about 61% of Rails developers use RSpec (http://rspec.info), whereas only about 24% prefer the built-in Test Unit framework that is shipped with Rails. RSpec is definitely worth looking into, as are other methods of testing that I don’t cover in this book such as test-driven development (http://www.testdriven.com) and Selenium IDE for Firefox (http://selenium.openqa.org).

In Rails, these three types of tests have very specific meanings that may differ from what you expect:

Functional tests

Used for testing controllers

Unit tests

Used for testing models

Integration tests

Used for testing higher-level scenarios that exercise interactions between controllers

1.7.1. Assertions

An assertion is a single line of code that evaluates an expression and tests the results against an expected value. For example, you might assert that a password is at least six characters long; failing an assertion fails the associated test.

Rails currently has six categories of assertions.
DOM assertions

assert_dom_equal(expected, actual, message = "")

Tests two HTML strings for equivalency (e.g., identical up to reordering ...

Get Rails Pocket Reference now with O’Reilly online learning.

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