Skip to content
This repository

Stringify param values in controller tests. #1203

Merged
merged 2 commits into from about 3 years ago

5 participants

David Chelimsky Andrew White José Valim Akira Matsuda Michael Koziarski
David Chelimsky

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
David Chelimsky dchelimsky 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
9277e72
Andrew White
Owner

Wouldn't it be better to call to_param? To save rehashing old arguments look at these tickets:

https://rails.lighthouseapp.com/projects/8994/tickets/2984
https://rails.lighthouseapp.com/projects/8994/tickets/2970

José Valim
Owner

to_param would definitely be more appropriate as we would be able to pass models to get/post and friends. @dchelimsky, what do you think?

David Chelimsky

to_param makes sense (I hadn't seen those tickets, sry). That said, I'm a bit confused. https://rails.lighthouseapp.com/projects/8994/tickets/2984 says it's resolved, but I don't see a patch. What happened?

Also - we need to exclude Rack::Test::UploadedFile from this as the to_param conversion does the wrong thing. Are there any other exclusions you can think of?

David Chelimsky dchelimsky use to_param (and change method to name accordingly)
- exclude Rack::Test::UploadedFile to pass existing tests. Are there any
  other types we're missing?
3f0c71c
Akira Matsuda
Collaborator

+1 for to_paramification.

José Valim josevalim merged commit a46b03e into from
José Valim josevalim closed this
David Chelimsky

@josevalim - I see this is in master but not 3-1-stable. Is the intent to include this post 3.1?

José Valim
Owner

@dchelimsky we can include it on 3-1. Could you please send a pull request for 3-1-stable (I am without my mac lately)?

David Chelimsky

@josevalim - I squashed the two commits into a single commit and cherry-picked it onto 3-1-stable in a separate pull request: #2253

David Chelimsky

Per https://twitter.com/#!/nzkoz/status/177832158617018368, this breaks the ability to functional test json APIs (which support fixnums). Any thoughts on how to preserve the benefits from this change and also support json APIs? The first thing that comes to my mind is a declaration in the test like do_not_convert_params or some such. I'd expect the conversion to be an opt-out, but that's me :)

Michael Koziarski
Owner
NZKoz commented

Well, this change breaks backwards compatibility and should only be applied when it's definitely safe, but that's me ;)

I think the simplest option is to only do this to_paramization when the format of the request is :html or nil, that at least lets people include :format=>:json and hopefully move on with their lives?

Michael Koziarski
Owner
NZKoz commented

I'd suggest discussing in that issue rather than here as this one is closed and ancient

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 2 unique commits by 1 author.

May 22, 2011
David Chelimsky dchelimsky 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
9277e72
David Chelimsky dchelimsky use to_param (and change method to name accordingly)
- exclude Rack::Test::UploadedFile to pass existing tests. Are there any
  other types we're missing?
3f0c71c
This page is out of date. Refresh to see the latest.
19 actionpack/lib/action_controller/test_case.rb
@@ -395,7 +395,26 @@ def xml_http_request(request_method, action, parameters = nil, session = nil, fl
395 395 end
396 396 alias xhr :xml_http_request
397 397
  398 + def paramify_values(hash_or_array_or_value)
  399 + case hash_or_array_or_value
  400 + when Hash
  401 + hash_or_array_or_value.each do |key, value|
  402 + hash_or_array_or_value[key] = paramify_values(value)
  403 + end
  404 + when Array
  405 + hash_or_array_or_value.map {|i| paramify_values(i)}
  406 + when Rack::Test::UploadedFile
  407 + hash_or_array_or_value
  408 + else
  409 + hash_or_array_or_value.to_param
  410 + end
  411 + end
  412 +
398 413 def process(action, parameters = nil, session = nil, flash = nil, http_method = 'GET')
  414 + # Ensure that numbers and symbols passed as params are converted to
  415 + # proper params, as is the case when engaging rack.
  416 + paramify_values(parameters)
  417 +
399 418 # Sanity check for required instance variables so we can give an
400 419 # understandable error message.
401 420 %w(@routes @controller @request @response).each do |iv_name|
10 actionpack/test/controller/test_test.rb
@@ -493,6 +493,16 @@ def test_params_passing
493 493 )
494 494 end
495 495
  496 + def test_params_passing_with_fixnums
  497 + get :test_params, :page => {:name => "Page name", :month => 4, :year => 2004, :day => 6}
  498 + parsed_params = eval(@response.body)
  499 + assert_equal(
  500 + {'controller' => 'test_test/test', 'action' => 'test_params',
  501 + 'page' => {'name' => "Page name", 'month' => '4', 'year' => '2004', 'day' => '6'}},
  502 + parsed_params
  503 + )
  504 + end
  505 +
496 506 def test_params_passing_with_frozen_values
497 507 assert_nothing_raised do
498 508 get :test_params, :frozen => 'icy'.freeze, :frozens => ['icy'.freeze].freeze

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.