I'm writing controller tests in Rails and RSpec, and it seems from reading the source code of ActionController::TestCase that it's not possible to pass arbitrary query parameters to the controller -- only routing parameters.
To work around this limitation, I am currently using with_routing:
with_routing do |routes| # this nonsense is necessary because # Rails controller testing does not # pass on query params, only routing params routes.draw do get '/users/confirmation/:confirmation_token' => 'user_confirmations#show' root :to => 'root#index' end get :show, 'confirmation_token' => CONFIRMATION_TOKEN end As you may be able to guess, I am testing a custom Confirmations controller for Devise. This means I am jacking into an existing API and do not have the option to change how the real mapping in config/routes.rb is done.
Is there a neater way to do this? A supported way for get to pass query parameters?
EDIT: There is something else going on. I created a minimal example in https://github.com/clacke/so_13866283 :
spec/controllers/receive_query_param_controller_spec.rb
describe ReceiveQueryParamController do describe '#please' do it 'receives query param, sets @my_param' do get :please, :my_param => 'test_value' assigns(:my_param).should eq 'test_value' end end end app/controllers/receive_query_param_controller.rb
class ReceiveQueryParamController < ApplicationController def please @my_param = params[:my_param] end end config/routes.rb
So13866283::Application.routes.draw do get '/receive_query_param/please' => 'receive_query_param#please' end This test passes, so I suppose it is Devise that does something funky with the routing.
EDIT:
Pinned down where in Devise routes are defined, and updated my example app to match it.
So13866283::Application.routes.draw do resource :receive_query_param, :only => [:show], :controller => "receive_query_param" end ... and spec and controller updated accordingly to use #show. The test still passes, i.e. params[:my_param] is populated by get :show, :my_param => 'blah'. So, still a mystery why this does not happen in my real app.
getas well.query_parametersis defined and how I can populate it.GETand its aliasquery_parametersare defined in github.com/rails/rails/blob/v3.2.8/actionpack/lib/… and override theGETinRack::Request.