Skip to content
This repository
Browse code

assert_response failures include the exception message. Closes #10688.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8559 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit 3464a7e79c373947ea7d57c543e36f41bd62049e 1 parent 5ccc3cf
Jeremy Kemper jeremy authored
2  actionpack/CHANGELOG
... ... @@ -1,5 +1,7 @@
1 1 *SVN*
2 2
  3 +* assert_response failures include the exception message. #10688 [Seth Rasmussen]
  4 +
3 5 * All fragment cache keys are now by default prefixed with the "views/" namespace [DHH]
4 6
5 7 * Moved the caching stores from ActionController::Caching::Fragments::* to ActiveSupport::Cache::*. If you're explicitly referring to a store, like ActionController::Caching::Fragments::MemoryStore, you need to update that reference with ActiveSupport::Cache::MemoryStore [DHH]
6 actionpack/lib/action_controller/assertions/response_assertions.rb
@@ -33,7 +33,11 @@ def assert_response(type, message = nil)
33 33 elsif type.is_a?(Symbol) && @response.response_code == ActionController::StatusCodes::SYMBOL_TO_STATUS_CODE[type]
34 34 assert_block("") { true } # to count the assertion
35 35 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
37 41 end
38 42 end
39 43 end
18 actionpack/test/controller/action_pack_assertions_test.rb
@@ -124,6 +124,15 @@ def full_messages; ['...stuff...']; end
124 124 def rescue_action(e) raise; end
125 125 end
126 126
  127 +# Used to test that assert_response includes the exception message
  128 +# in the failure message when an action raises and assert_response
  129 +# is expecting something other than an error.
  130 +class AssertResponseWithUnexpectedErrorController < ActionController::Base
  131 + def index
  132 + raise 'FAIL'
  133 + end
  134 +end
  135 +
127 136 module Admin
128 137 class InnerModuleController < ActionController::Base
129 138 def index
@@ -465,6 +474,15 @@ def test_assert_valid_failing
465 474 rescue Test::Unit::AssertionFailedError => e
466 475 end
467 476 end
  477 +
  478 + def test_assert_response_uses_exception_message
  479 + @controller = AssertResponseWithUnexpectedErrorController.new
  480 + get :index
  481 + assert_response :success
  482 + flunk 'Expected non-success response'
  483 + rescue Test::Unit::AssertionFailedError => e
  484 + assert e.message.include?('FAIL')
  485 + end
468 486 end
469 487
470 488 class ActionPackHeaderTest < Test::Unit::TestCase

0 comments on commit 3464a7e

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