diff options
author | Arthur Nogueira Neves <github@arthurnn.com> | 2015-12-11 13:12:33 -0500 |
---|---|---|
committer | Arthur Nogueira Neves <github@arthurnn.com> | 2015-12-11 13:12:33 -0500 |
commit | eb0e8e216fcf535a1e6b82720dfb7639fcc20ff2 (patch) | |
tree | f1d238e72d4fd1fc84095ed629dadafe96d55d8c | |
parent | a42770cdd806d141ae8de1fa879703967532da85 (diff) | |
parent | c6fe614e45a4ae40bb635c1259423ef9e776dd40 (diff) | |
download | rails-eb0e8e216fcf535a1e6b82720dfb7639fcc20ff2.tar.gz rails-eb0e8e216fcf535a1e6b82720dfb7639fcc20ff2.tar.bz2 rails-eb0e8e216fcf535a1e6b82720dfb7639fcc20ff2.zip |
Merge pull request #22511 from jonatack/redirect-to-302
Show redirect response code in assert_response messages
-rw-r--r-- | actionpack/lib/action_dispatch/testing/assertions/response.rb | 18 | ||||
-rw-r--r-- | actionpack/test/assertions/response_assertions_test.rb | 35 |
2 files changed, 36 insertions, 17 deletions
diff --git a/actionpack/lib/action_dispatch/testing/assertions/response.rb b/actionpack/lib/action_dispatch/testing/assertions/response.rb index cc2a2fb119..c138660a21 100644 --- a/actionpack/lib/action_dispatch/testing/assertions/response.rb +++ b/actionpack/lib/action_dispatch/testing/assertions/response.rb @@ -85,17 +85,15 @@ module ActionDispatch 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 + def generate_response_message(type, code = @response.response_code) + "Expected response to be a <#{type}>, but was a <#{code}>" + .concat location_if_redirected + end - message + def location_if_redirected + return '' unless @response.redirection? && @response.location.present? + location = normalize_argument_to_redirection(@response.location) + " redirect to <#{location}>" end end end diff --git a/actionpack/test/assertions/response_assertions_test.rb b/actionpack/test/assertions/response_assertions_test.rb index 00fb1c0f12..e76c222824 100644 --- a/actionpack/test/assertions/response_assertions_test.rb +++ b/actionpack/test/assertions/response_assertions_test.rb @@ -64,14 +64,35 @@ module ActionDispatch } 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 + def test_error_message_shows_404_when_404_asserted_for_success + @response = ActionDispatch::Response.new + @response.status = 404 + + error = assert_raises(Minitest::Assertion) { assert_response :success } + expected = "Expected response to be a <success>, but was a <404>" + assert_match expected, error.message + end + + def test_error_message_shows_302_redirect_when_302_asserted_for_success + @response = ActionDispatch::Response.new + @response.status = 302 + @response.location = 'http://test.host/posts/redirect/1' + + error = assert_raises(Minitest::Assertion) { assert_response :success } + expected = "Expected response to be a <success>, but was a <302>" \ + " redirect to <http://test.host/posts/redirect/1>" + assert_match expected, error.message + end + + def test_error_message_shows_302_redirect_when_302_asserted_for_301 + @response = ActionDispatch::Response.new + @response.status = 302 + @response.location = 'http://test.host/posts/redirect/2' - expected = "Expected response to be a <success>, but was a redirect to <http://test.host/posts/redirect/1>" - assert_equal expected, error.message + error = assert_raises(Minitest::Assertion) { assert_response 301 } + expected = "Expected response to be a <301>, but was a <302>" \ + " redirect to <http://test.host/posts/redirect/2>" + assert_match expected, error.message end end end |