diff options
-rw-r--r-- | actionpack/lib/action_dispatch/testing/assertions/response.rb | 15 | ||||
-rw-r--r-- | actionpack/test/assertions/response_assertions_test.rb | 17 |
2 files changed, 31 insertions, 1 deletions
diff --git a/actionpack/lib/action_dispatch/testing/assertions/response.rb b/actionpack/lib/action_dispatch/testing/assertions/response.rb index eab20b075d..5af052afb4 100644 --- a/actionpack/lib/action_dispatch/testing/assertions/response.rb +++ b/actionpack/lib/action_dispatch/testing/assertions/response.rb @@ -27,6 +27,8 @@ module ActionDispatch # # Asserts that the response code was status code 401 (unauthorized) # assert_response 401 def assert_response(type, message = nil) + message ||= generate_response_message(type) + if Symbol === type if [:success, :missing, :redirect, :error].include?(type) assert_predicate @response, RESPONSE_PREDICATES[type], message @@ -82,6 +84,19 @@ module ActionDispatch handle._compute_redirect_to_location(@request, fragment) end end + + def generate_response_message(type) + message = "Expected response to be a <#{type}>, but was" + + if @response.redirection? + redirect_is = normalize_argument_to_redirection(@response.location) + message << " a redirect to <#{redirect_is}>" + else + message << " <#{@response.response_code}>" + end + + message + end end end end diff --git a/actionpack/test/assertions/response_assertions_test.rb b/actionpack/test/assertions/response_assertions_test.rb index 82c747680d..6c7036aa1a 100644 --- a/actionpack/test/assertions/response_assertions_test.rb +++ b/actionpack/test/assertions/response_assertions_test.rb @@ -6,7 +6,12 @@ module ActionDispatch class ResponseAssertionsTest < ActiveSupport::TestCase include ResponseAssertions - FakeResponse = Struct.new(:response_code) do + FakeResponse = Struct.new(:response_code, :location) do + def initialize(*) + super + self.location ||= "http://test.example.com/posts" + end + [:successful, :not_found, :redirection, :server_error].each do |sym| define_method("#{sym}?") do sym == response_code @@ -58,6 +63,16 @@ module ActionDispatch assert_response :succezz } end + + def test_message_when_response_is_redirect_but_asserted_for_status_other_than_redirect + @response = FakeResponse.new :redirection, "http://test.host/posts/redirect/1" + error = assert_raises(Minitest::Assertion) do + assert_response :success + end + + expected = "Expected response to be a <success>, but was a redirect to <http://test.host/posts/redirect/1>." + assert_match expected, error.message + end end end end |