Skip to content

Instantly share code, notes, and snippets.

@dimitarvp
Created April 2, 2014 23:07
Show Gist options
  • Save dimitarvp/9945140 to your computer and use it in GitHub Desktop.
Save dimitarvp/9945140 to your computer and use it in GitHub Desktop.
Cannot modify `request.params` in before(:all) block.
# Given this auth helper in spec/support/ directory...
module AuthHelpers
def add_token_to_params(token)
request.params.merge!({sessionToken: token}) # also tried 'request.params[:sessionToken] = token'
end
end
# Given this test...
class ThingController
describe Thing do
before(:all) do
@user = User.authenticate(Settings.tests.user, Settings.tests.password)
@token = @user.attributes["sessionToken"]
end
describe "attempt to create Thing without session token" do
before(:each) { add_token_to_params(@token) } # Yes, I did inspect it here, the @token has a valid value. ;)
it "should fail" do
puts request.params # Shows {"sessionToken"=>"abc"}
post :create, attr1: 1, attr2: 'abc' # etc.
puts request.params # Shows {"attr1"=>1, "attr2"=>"abc"}
# failure here; my application_controller before_filter that enforces the presence of
# the 'sessionToken' parameter kicks in and returns '401 Unauthorized'.
response.should be_success
end
end
end
end
# ...I want to freely modify request parameters before invoking controller actions.
# (I know I can modify the headers, btw; tried it and it works.)
# I am 99% certain I am doing it the wrong way, but couldn't find anything in Google
# that actually covers my requirements.
# The problem is not just to make this work in one "it" block; I have dozens of mini tests.
# I know I can make this work using simpler methods for just one "it" block, but that's not my scenario.
@dimitarvp
Copy link
Author

That did it. I am just not that well familiarized with the RSpec lingo yet. Thanks a lot for your support, it's highly appreciated. =)

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