Skip to content

Commit 908bc79

Browse files
committed
use a lookup table for assert_response
We shouldn't depend on specific methods imlemented in the TestResponse subclass because the response could actually be a real response object. In the future, we should either push the aliased predicate methods in TestResponse up to the real response object, or remove them
1 parent d9fe10c commit 908bc79

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

actionpack/lib/action_dispatch/testing/assertions/response.rb

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@ module ActionDispatch
33
module Assertions
44
# A small suite of assertions that test responses from \Rails applications.
55
module ResponseAssertions
6+
RESPONSE_PREDICATES = { # :nodoc:
7+
success: :successful?,
8+
missing: :not_found?,
9+
redirect: :redirection?,
10+
error: :server_error?,
11+
}
12+
613
# Asserts that the response is one of the following types:
714
#
815
# * <tt>:success</tt> - Status code was in the 200-299 range
@@ -20,11 +27,9 @@ module ResponseAssertions
2027
# # assert that the response code was status code 401 (unauthorized)
2128
# assert_response 401
2229
def assert_response(type, message = nil)
23-
message ||= "Expected response to be a <#{type}>, but was <#{@response.response_code}>"
24-
2530
if Symbol === type
2631
if [:success, :missing, :redirect, :error].include?(type)
27-
assert @response.send("#{type}?"), message
32+
assert_predicate @response, RESPONSE_PREDICATES[type], message
2833
else
2934
code = Rack::Utils::SYMBOL_TO_STATUS_CODE[type]
3035
if code.nil?

actionpack/test/assertions/response_assertions_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class ResponseAssertionsTest < ActiveSupport::TestCase
77
include ResponseAssertions
88

99
FakeResponse = Struct.new(:response_code) do
10-
[:success, :missing, :redirect, :error].each do |sym|
10+
[:successful, :not_found, :redirection, :server_error].each do |sym|
1111
define_method("#{sym}?") do
1212
sym == response_code
1313
end
@@ -16,7 +16,7 @@ class ResponseAssertionsTest < ActiveSupport::TestCase
1616

1717
def test_assert_response_predicate_methods
1818
[:success, :missing, :redirect, :error].each do |sym|
19-
@response = FakeResponse.new sym
19+
@response = FakeResponse.new RESPONSE_PREDICATES[sym].to_s.sub(/\?/, '').to_sym
2020
assert_response sym
2121

2222
assert_raises(Minitest::Assertion) {

0 commit comments

Comments
 (0)