Skip to content
This repository
Browse code

Improve overview for ActionController::TestCase.

  • Loading branch information...
commit 03afe79dd8ea8ad968c4921c233aacc7eca02f5a 1 parent 5508bfd
Hongli Lai FooBarWidget authored
62 actionpack/lib/action_controller/test_case.rb
@@ -15,23 +15,61 @@ def inferred_controller_name
15 15 end
16 16 end
17 17
18   - # Superclass for Action Controller functional tests. Infers the controller under test from the test class name,
19   - # and creates @controller, @request, @response instance variables.
  18 + # Superclass for ActionController functional tests. Functional tests allow you to
  19 + # test a single controller action per test method. This should not be confused with
  20 + # integration tests (see ActionController::IntegrationTest), which are more like
  21 + # "stories" that can involve multiple controllers and mutliple actions (i.e. multiple
  22 + # different HTTP requests).
20 23 #
21   - # class WidgetsControllerTest < ActionController::TestCase
22   - # def test_index
23   - # get :index
  24 + # == Basic example
  25 + #
  26 + # Functional tests are written as follows:
  27 + # 1. First, one uses the +get+, +post+, +put+, +delete+ or +head+ method to simulate
  28 + # an HTTP request.
  29 + # 2. Then, one asserts whether the current state is as expected. "State" can be anything:
  30 + # the controller's HTTP response, the database contents, etc.
  31 + #
  32 + # For example:
  33 + #
  34 + # class BooksControllerTest < ActionController::TestCase
  35 + # def test_create
  36 + # # Simulate a POST response with the given HTTP parameters.
  37 + # post(:create, :book => { :title => "Love Hina" })
  38 + #
  39 + # # Assert that the controller tried to redirect us to
  40 + # # the created book's URI.
  41 + # assert_response :found
  42 + #
  43 + # # Assert that the controller really put the book in the database.
  44 + # assert_not_nil Book.find_by_title("Love Hina")
24 45 # end
25 46 # end
26 47 #
27   - # * @controller - WidgetController.new
28   - # * @request - ActionController::TestRequest.new
29   - # * @response - ActionController::TestResponse.new
  48 + # == Special instance variables
  49 + #
  50 + # ActionController::TestCase will also automatically provide the following instance
  51 + # variables for use in the tests:
  52 + #
  53 + # <b>@controller</b>::
  54 + # The controller instance that will be tested.
  55 + # <b>@request</b>::
  56 + # An ActionController::TestRequest, representing the current HTTP
  57 + # request. You can modify this object before sending the HTTP request. For example,
  58 + # you might want to set some session properties before sending a GET request.
  59 + # <b>@response</b>::
  60 + # An ActionController::TestResponse object, representing the response
  61 + # of the last HTTP response. In the above example, <tt>@response</tt> becomes valid
  62 + # after calling +post+. If the various assert methods are not sufficient, then you
  63 + # may use this object to inspect the HTTP response in detail.
  64 + #
  65 + # (Earlier versions of Rails required each functional test to subclass
  66 + # Test::Unit::TestCase and define @controller, @request, @response in +setup+.)
30 67 #
31   - # (Earlier versions of Rails required each functional test to subclass Test::Unit::TestCase and define
32   - # @controller, @request, @response in +setup+.)
  68 + # == Controller is automatically inferred
33 69 #
34   - # If the controller cannot be inferred from the test class name, you can explicity set it with +tests+.
  70 + # ActionController::TestCase will automatically infer the controller under test
  71 + # from the test class name. If the controller cannot be inferred from the test
  72 + # class name, you can explicity set it with +tests+.
35 73 #
36 74 # class SpecialEdgeCaseWidgetsControllerTest < ActionController::TestCase
37 75 # tests WidgetController
@@ -103,4 +141,4 @@ def rescue_action_in_public!
103 141 @request.remote_addr = '208.77.188.166' # example.com
104 142 end
105 143 end
106   -end
  144 +end
1  actionpack/lib/action_controller/test_process.rb
@@ -361,6 +361,7 @@ def method_missing(method_name, *args, &block) #:nodoc:
361 361 module TestProcess
362 362 def self.included(base)
363 363 # execute the request simulating a specific HTTP method and set/volley the response
  364 + # TODO: this should be un-DRY'ed for the sake of API documentation.
364 365 %w( get post put delete head ).each do |method|
365 366 base.class_eval <<-EOV, __FILE__, __LINE__
366 367 def #{method}(action, parameters = nil, session = nil, flash = nil)

0 comments on commit 03afe79

Please sign in to comment.
Something went wrong with that request. Please try again.