Class: ActionController::TestCase

  • Object
show all
Defined in:


Superclass for ActionController functional tests. Functional tests allow you to test a single controller action per test method. This should not be confused with integration tests (see ActionController::IntegrationTest), which are more like “stories” that can involve multiple controllers and multiple actions (i.e. multiple different HTTP requests).

Basic example

Functional tests are written as follows:


  2. Then, one asserts whether the current state is as expected. “State” can be anything: the controller's HTTP response, the database contents, etc.

For example:

class BooksControllerTest < ActionController::TestCase
  def test_create
    # Simulate a POST response with the given HTTP parameters.

    # Assert that the controller tried to redirect us to
    # the created book's URI.
    assert_response :found

    assert_not_nil Book.find_by_title("Love Hina")

Special instance variables



The controller instance that will be tested.






Controller is automatically inferred


class SpecialEdgeCaseWidgetsControllerTest < ActionController::TestCase
  tests WidgetController

Testing controller internals



  • session: Objects being saved in the session.

  • flash: The flash objects currently in the session.


These collections can be used just like any other hash:

assert flash.empty? # makes sure that there's nothing in the flash

For historic reasons, the assigns hash uses string-based keys. So assigns won't work, but assigns will. To appease our yearning for symbols, though, an alternative accessor has been devised using a method call instead of index referencing. So assigns(:person) will work just like assigns, but again, assigns will not work.



Manipulating the request collections

The collections described above link to the response, so you can test if what the actions were expected to do happened. But sometimes you also want to manipulate these collections in the incoming request. This is really only relevant for sessions and cookies, though. For sessions, you just do:

@request.session[:key] = "value"
@request.cookies["key"] = "value"

Testing named routes

If you're using named routes, they can be easily tested using the original named routes' methods straight in the test case. Example:

assert_redirected_to page_url(:title => 'foo')

Defined Under Namespace

Modules: Behavior, RaiseActionExceptions

Instance Attribute Summary

Attributes included from Behavior

#request, #response

Method Summary

Methods included from Behavior

#delete, #get, #head, #post, #process, #put, #rescue_action_in_public!, #setup_controller_request_and_response, #xml_http_request

Methods included from ActionDispatch::TestProcess

#assigns, #cookies, #fixture_file_upload, #flash, #redirect_to_url, #session