O'Reilly logo

Ajax on Rails by Scott Raymond

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Testing

Automated testing is a development practice that Rails—as opinioned software—believes in strongly. Rails divides tests into three groups: unit tests, which cover your models; functional tests, which cover your controllers; and integration tests, which also exercise controllers, but at a higher level. Since ActiveRecord is outside of the scope of this book, we won’t look at unit tests, instead focusing on functional and integration tests.

Functional Tests

The goal of functional testing is to isolate each action in your controllers and verify that they behave as expected. As the simplest level, that means providing some amount of input (in the form of fixtures, sessions, query parameters, or request body) and then verifying the result (e.g., response body, headers, session, database changes).

To accomplish that, Rails uses Ruby’s standard testing framework, Test::Unit. Let’s look at an example. Suppose you have a simple, one-action controller with a before filter, like this:

class PeopleController < ApplicationController
  
  before_filter :require_login

  def index
    @people = Person.find :all
  end

end

To make sure that it works, at least roughly, we’d create a test like this:

class PeopleControllerTest < Test::Unit::TestCase fixtures :people def setup @controller = PeopleController.new @request = ActionController::TestRequest.new @response = ActionController::TestResponse.new login end def test_index get :index assert_response :success assert_template 'index' assert_not_nil assigns(:people) ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required