Permalink
Browse files

Stringify param values in controller tests.

This reduces false positives that come from using ints in params in
tests, which do not get converted to strings in the tests. In
implementations going through rack, they do get converted to strings.

- David Chelimsky and Sam Umbach
  • Loading branch information...
1 parent 82857ad commit 9277e72a3ca1009f135b1eb194ff6f3c96a55c1b @dchelimsky dchelimsky committed May 22, 2011
Showing with 29 additions and 0 deletions.
  1. +19 −0 actionpack/lib/action_controller/test_case.rb
  2. +10 −0 actionpack/test/controller/test_test.rb
View
19 actionpack/lib/action_controller/test_case.rb
@@ -395,7 +395,26 @@ def xml_http_request(request_method, action, parameters = nil, session = nil, fl
end
alias xhr :xml_http_request
+ def stringify_values(hash_or_array_or_value)
+ case hash_or_array_or_value
+ when Hash
+ hash_or_array_or_value.each do |key, value|
+ hash_or_array_or_value[key] = stringify_values(value)
+ end
+ when Array
+ hash_or_array_or_value.map {|i| stringify_values(i)}
+ when Numeric, Symbol
+ hash_or_array_or_value.to_s
+ else
+ hash_or_array_or_value
+ end
+ end
+
def process(action, parameters = nil, session = nil, flash = nil, http_method = 'GET')
+ # Ensure that numbers and symbols passed as params are converted to
+ # strings, as is the case when engaging rack.
+ stringify_values(parameters)
+
# Sanity check for required instance variables so we can give an
# understandable error message.
%w(@routes @controller @request @response).each do |iv_name|
View
10 actionpack/test/controller/test_test.rb
@@ -493,6 +493,16 @@ def test_params_passing
)
end
+ def test_params_passing_with_fixnums
+ get :test_params, :page => {:name => "Page name", :month => 4, :year => 2004, :day => 6}
+ parsed_params = eval(@response.body)
+ assert_equal(
+ {'controller' => 'test_test/test', 'action' => 'test_params',
+ 'page' => {'name' => "Page name", 'month' => '4', 'year' => '2004', 'day' => '6'}},
+ parsed_params
+ )
+ end
+
def test_params_passing_with_frozen_values
assert_nothing_raised do
get :test_params, :frozen => 'icy'.freeze, :frozens => ['icy'.freeze].freeze

0 comments on commit 9277e72

Please sign in to comment.