Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #10688: assert_response_with_exception_message_for_500.diff

File assert_response_with_exception_message_for_500.diff, 3.3 kB (added by loincloth, 9 months ago)
  • actionpack/test/controller/action_pack_assertions_test.rb

    old new  
    2727 
    2828  # a redirect to an external location 
    2929  def redirect_external() redirect_to "http://www.rubyonrails.org"; end 
    30  
    31   # a 404 
    32   def response404() head '404 AWOL' end 
    33  
    34   # a 500 
    35   def response500() head '500 Sorry' end 
    36  
     30   
     31  ActionController::StatusCodes::STATUS_CODES.each do |code,message| 
     32    define_method("response#{code}") { head "#{code} #{message}" } 
     33  end 
     34   
    3735  # a fictional 599 
    3836  def response599() head '599 Whoah!' end 
    39  
     37   
    4038  # putting stuff in the flash 
    4139  def flash_me 
    4240    flash['hello'] = 'my name is inigo montoya...' 
     
    124122  def rescue_action(e) raise; end 
    125123end 
    126124 
     125# Used to test that assert_response includes the exception message 
     126# in the failure message when an action raises and assert_response  
     127# is expecting something other than an error. 
     128class AssertResponseWithUnexpectedErrorController < ActionController::Base 
     129  def index 
     130    raise 'FAIL' 
     131  end 
     132end 
     133 
    127134module Admin 
    128135  class InnerModuleController < ActionController::Base 
    129136    def index 
     
    465472    rescue Test::Unit::AssertionFailedError => e 
    466473    end 
    467474  end 
     475   
     476  ActionController::StatusCodes::STATUS_CODES.keys.each do |code| 
     477    define_method "test assert_response #{code}" do 
     478      get "response#{code}" 
     479      assert_response code 
     480    end 
     481  end 
     482   
     483  def test_assert_response_uses_exception_message 
     484    @controller = AssertResponseWithUnexpectedErrorController.new 
     485    get :index 
     486    begin 
     487      assert_response :success 
     488    rescue Test::Unit::AssertionFailedError => e 
     489      assert e.message.include?('FAIL') 
     490    end 
     491  end 
    468492end 
    469493 
    470494class ActionPackHeaderTest < Test::Unit::TestCase 
  • actionpack/lib/action_controller/assertions/response_assertions.rb

    old new  
    3333          elsif type.is_a?(Symbol) && @response.response_code == ActionController::StatusCodes::SYMBOL_TO_STATUS_CODE[type] 
    3434            assert_block("") { true } # to count the assertion 
    3535          else 
    36             assert_block(build_message(message, "Expected response to be a <?>, but was <?>", type, @response.response_code)) { false } 
     36            if @response.error? 
     37              assert_block(build_message(message, "Expected response to be a <?>, but was <?>\n<?>", type, @response.response_code, @response.template.instance_variable_get(:@exception).message)) { false } 
     38            else 
     39              assert_block(build_message(message, "Expected response to be a <?>, but was <?>", type, @response.response_code)) { false }  
     40            end 
    3741          end 
    3842        end 
    3943      end 
    40  
     44       
    4145      # Assert that the redirection options passed in match those of the redirect called in the latest action.  
    4246      # This match can be partial, such that assert_redirected_to(:controller => "weblog") will also 
    4347      # match the redirection of redirect_to(:controller => "weblog", :action => "show") and so on.