test_case.rb 5.4 KB
Newer Older
1 2 3 4 5
require 'active_support/test_case'

module ActionController
  class NonInferrableControllerError < ActionControllerError
    def initialize(name)
6
      @name = name
7
      super "Unable to determine the controller to test from #{name}. " +
8
        "You'll need to specify it using 'tests YourController' in your " +
9 10 11 12 13 14
        "test case definition. This could mean that #{inferred_controller_name} does not exist " +
        "or it contains syntax errors"
    end

    def inferred_controller_name
      @name.sub(/Test$/, '')
15 16 17
    end
  end

P
Pratik Naik 已提交
18 19 20 21 22
  # 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 mutliple actions (i.e. multiple
  # different HTTP requests).
P
Pratik Naik 已提交
23
  #
P
Pratik Naik 已提交
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
  # == Basic example
  #
  # Functional tests are written as follows:
  # 1. First, one uses the +get+, +post+, +put+, +delete+ or +head+ method to simulate
  #    an HTTP request.
  # 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.
  #       post(:create, :book => { :title => "Love Hina" })
  #
  #       # Assert that the controller tried to redirect us to
  #       # the created book's URI.
  #       assert_response :found
  #
  #       # Assert that the controller really put the book in the database.
  #       assert_not_nil Book.find_by_title("Love Hina")
P
Pratik Naik 已提交
45 46 47
  #     end
  #   end
  #
P
Pratik Naik 已提交
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
  # == Special instance variables
  #
  # ActionController::TestCase will also automatically provide the following instance
  # variables for use in the tests:
  #
  # <b>@controller</b>::
  #      The controller instance that will be tested.
  # <b>@request</b>::
  #      An ActionController::TestRequest, representing the current HTTP
  #      request. You can modify this object before sending the HTTP request. For example,
  #      you might want to set some session properties before sending a GET request.
  # <b>@response</b>::
  #      An ActionController::TestResponse object, representing the response
  #      of the last HTTP response. In the above example, <tt>@response</tt> becomes valid
  #      after calling +post+. If the various assert methods are not sufficient, then you
  #      may use this object to inspect the HTTP response in detail.
  #
  # (Earlier versions of Rails required each functional test to subclass
  # Test::Unit::TestCase and define @controller, @request, @response in +setup+.)
P
Pratik Naik 已提交
67
  #
P
Pratik Naik 已提交
68
  # == Controller is automatically inferred
P
Pratik Naik 已提交
69
  #
P
Pratik Naik 已提交
70 71 72
  # ActionController::TestCase will automatically infer the controller under test
  # from the test class name. If the controller cannot be inferred from the test
  # class name, you can explicity set it with +tests+.
P
Pratik Naik 已提交
73 74 75 76
  #
  #   class SpecialEdgeCaseWidgetsControllerTest < ActionController::TestCase
  #     tests WidgetController
  #   end
77
  class TestCase < ActiveSupport::TestCase
78 79 80 81 82 83
    # When the request.remote_addr remains the default for testing, which is 0.0.0.0, the exception is simply raised inline
    # (bystepping the regular exception handling from rescue_action). If the request.remote_addr is anything else, the regular
    # rescue_action process takes place. This means you can test your rescue_action code by setting remote_addr to something else
    # than 0.0.0.0.
    #
    # The exception is stored in the exception accessor for further inspection.
84
    module RaiseActionExceptions
85 86
      attr_accessor :exception

87
      def rescue_action(e)
88 89 90 91 92 93 94
        self.exception = e
        
        if request.remote_addr == "0.0.0.0"
          raise(e)
        else
          super(e)
        end
95 96 97 98 99
      end
    end

    setup :setup_controller_request_and_response

100
    @@controller_class = nil
101

102
    class << self
P
Pratik Naik 已提交
103 104
      # Sets the controller class name. Useful if the name can't be inferred from test class.
      # Expects +controller_class+ as a constant. Example: <tt>tests WidgetController</tt>.
105 106 107 108 109 110 111 112 113 114 115 116 117
      def tests(controller_class)
        self.controller_class = controller_class
      end

      def controller_class=(new_class)
        prepare_controller_class(new_class)
        write_inheritable_attribute(:controller_class, new_class)
      end

      def controller_class
        if current_controller_class = read_inheritable_attribute(:controller_class)
          current_controller_class
        else
118
          self.controller_class = determine_default_controller_class(name)
119 120 121 122 123 124 125 126 127 128
        end
      end

      def determine_default_controller_class(name)
        name.sub(/Test$/, '').constantize
      rescue NameError
        raise NonInferrableControllerError.new(name)
      end

      def prepare_controller_class(new_class)
129
        new_class.send :include, RaiseActionExceptions
130 131 132
      end
    end

133
    def setup_controller_request_and_response
134
      @controller = self.class.controller_class.new
135 136
      @controller.request = @request = TestRequest.new
      @response = TestResponse.new
137
    end
138 139 140 141 142
    
    # Cause the action to be rescued according to the regular rules for rescue_action when the visitor is not local
    def rescue_action_in_public!
      @request.remote_addr = '208.77.188.166' # example.com
    end
143
 end
P
Pratik Naik 已提交
144
end