Forum: Ruby on Rails Accessing session attributes from an integration test

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ddf9c98db9ac40c6875fd2a5b2d4fa72?d=identicon&s=25 Eric Northam (Guest)
on 2007-03-10 01:03
(Received via mailing list)
I have a very simple integration test based off of an example and
Agile Web Development with Rails but I can't seem to get it to work.
I'm simply setting a user id in the session via the get method in the
test but then can't access it when it hits my login controller.

Here is the relevant code in the integration test

---
get :index, {}, { :user_id => users(:bart).id }
assert_response :success
---
and in the login controller that gets called by the filter
---
def authorize
    puts "User ID from session[#{session[:user_id]}]"

    unless session[:user_id]
      flash[:notice] = "Please log in"
      redirect_to(:controller => 'login', :action => 'login')
    end
end
---
which prints out
---
#<ActionController::TestSession:0x38a08c4
 @attributes={:user_id=>1, "flash"=>{}},
 @saved_attributes=nil,
 @session_id="">
User ID from session[]
---

and then fails because the redirect is carried out. Anybody know why
the attribute is set but not accessible via session[:user_id]? The
code works fine when I manually try it out deployed.
E3640176765dae2465d33ef6eb114691?d=identicon&s=25 bcparanj@gmail.com (Guest)
on 2007-03-10 02:50
(Received via mailing list)
Make sure that you are using the right key for the user id. Run rake
db:sessions:clear, restart your server and try again.
Ddf9c98db9ac40c6875fd2a5b2d4fa72?d=identicon&s=25 Eric Northam (Guest)
on 2007-03-10 12:58
(Received via mailing list)
I had to change

get :index, {}, { :user_id => users(:bart).id }

to

get :index, {}, { 'user_id' => users(:bart).id }


The book example uses the original symbol version.
This topic is locked and can not be replied to.