Class Test::Rails::ControllerTestCase
In: lib/test/rails/controller_test_case.rb
Parent: Test::Rails::FunctionalTestCase

ControllerTestCase allows controllers to be tested independent of their views.

Features

  • ActionMailer is already set up for you.
  • The session and flash accessors work on both sides of get/post/etc.
  • Optional automatic auditing for missing assert_assigns. See util_audit_assert_assigned

Naming

The test class must be named after your controller class name, so if you‘re testing actions for the RouteController you would name your test case RouteControllerTest.

The test names should be in the form of test_action_edgecase where ‘action’ corresponds to the name of the controller action, and ‘edgecase’ describes the scenario you are testing.

If you are testing an action named ‘show’ your test should be named test_show. If your action behaves differently depending upon its arguments then you can make the test name descriptive like test_show_photos and test_show_no_photos.

Examples

Typical Controller Test

  class RouteControllerTest < Test::Rails::ControllerTestCase

    fixtures :users, :routes, :points, :photos

    def test_delete
      # Store current count
      count = Route.count
      # Set up our environment
      session[:username] = users(:herbert).username

      # perform the delete action
      get :delete, :id => routes(:work).id

      # Assert we got a 200
      assert_response :success
      # Assert controller deleted route
      assert_equal count-1, Route.count
      # Ensure that @action_title is set properly
      assert_assigned :action_title, "Deleting \"#{routes(:work).name}\""
      # Ensure that @route is set properly
      assert_assigned :route, routes(:work)
    end

  end

ActionMailer Test

  class ApplicationController < ActionController::Base

    ##
    # Send an email when we get an unhandled exception.

    def log_error(exception)
      case exception
      when ::ActionController::RoutingError,
           ::ActionController::UnknownAction,
           ::ActiveRecord::RecordNotFound then
        # ignore
      else
        unless RAILS_ENV == 'development' then
          Email.deliver_error exception, params, session, @request.env
        end
      end
    end

  end

  ##
  # Dummy Controller just for testing.

  class DummyController < ApplicationController

    def error
      # Simulate a bug in our application
      raise RuntimeError
    end

  end

  class DummyControllerTest < Test::Rails::ControllerTestCase

    def test_error_email
      # The rescue_action added by ControllerTestCase needs to be removed so
      # that exceptions fall through to the real error handler
      @controller.class.send :remove_method, :rescue_action

      # Fire off a request
      get :error

      # We should get a 500
      assert_response 500

      # And one delivered email
      assert_equal 1, @deliveries.length, 'error email sent'
    end

  end

Methods

Constants

NOTHING = Object.new # :nodoc:
DEFAULT_ASSIGNS = %w[ _cookies _flash _headers _params _request _response _session cookies flash headers params request response session action_name before_filter_chain_aborted db_rt_after_render db_rt_before_render ignore_missing_templates loggedin_user logger rendering_runtime request_origin template template_class template_root url user variables_added ]

Public Instance methods

Asserts that the assigns variable ivar is assigned to value. If value is omitted, asserts that assigns variable ivar exists.

Asserts the response content type matches type.

Asserts that key of flash has content. If content is a Regexp, then the assertion will fail if the Regexp does not match.

controller:

  flash[:notice] = 'Please log in'

test:

  assert_flash :notice, 'Please log in'

Performs a DELETE request on action with params.

Asserts that the assigns variable ivar is not set.

Performs a GET request on action with params.

Performs a HEAD request on action with params.

Performs a POST request on action with params.

Excutes the request action with params.

See also: get, post, put, delete, head, xml_http_request

Performs a PUT request on action with params.

Checks your assert_assigned tests against the instance variables in assigns. Fails if the two don‘t match.

Add util_audit_assert_assigned to your teardown. If you have instance variables that you don‘t need to set (for example, were set in a before_filter in ApplicationController) then add them to the @assigns_ignored instance variable in your setup.

Example

Controller method

  class UserController < ApplicationController
    def new
      # ...

      @login_form = false
    end
  end

Test setup:

  class UserControllerTest < Test::Rails::ControllerTestCase

    def teardown
      super
      util_audit_assert_assigned
    end

    def test_new
      get :new

      assert_response :success
      # no assert_assigns for @login_form
    end

  end

Output

    1) Failure:
  test_new(UserControllerTest)
      [[...]/controller_test_case.rb:331:in `util_audit_assert_assigned'
       [...]/user_controller_test.rb:14:in `teardown_without_fixtures'
       [...]fixtures.rb:555:in `teardown']:
  You are missing these assert_assigned assertions:
      assert_assigned :login_form #, some_value
  .
xhr(request_method, action, parameters = nil)

Alias for xml_http_request

Performs a XMLHttpRequest request using request_method on action with params.

[Validate]