Santiago Palladino

Santiago Palladino

Default request parameters in Rails functional tests

2 min
Dec 2 2011
coding, testing
2 min
Dec 2 2011

I was looking for an easy way to force every request in a functional test in Rails to use a set of parameters by default, regardless of being specified explicitly. This is, every time I write:

it "should get index" do
  get :index
end

Rails should actually do:

it "should get index" do
  get :index, :foo => 'value'
end

Lacking any option in the testing framework, I opted for simply monkeypatching the process method in ActionController::TestCase::Behaviour. This method is invoked whenever methods get, post, put, delete or head are called, so it is the easiest single point to modify.

This gist has the necessary code to perform the patch. If you are using rspec, simply copy the file in the spec/support folder.