Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access session from Rails Integration Test?

Consider the following integration test:

test "if there is no user in session, redirect to index and flash message" do
  open_session do |sess|
    post '/login', :email => users(:client).email, :password => 'rumpelstiltskin'
    sess[:user] = nil
    get '/user_page'
    assert_redirected_to index_path
    assert_equal "Your session has expired. Please log in again", flash[:notice]
  end    
end

This generates error: undefined method '[]='

And, changing to sess.session[:user] = nil generates an error as well: NoMethodError: undefined method 'session' for nil:NilClass

How do you modify the session params from an integration test in Rails?

Working in Rails 3.0.7, Ruby 1.9.2p180, Unit test framework.

EDIT:
get '/user_page', nil, {:user => nil} and get ('/user_page', nil, {:user => nil} ) generate errors.

like image 455
B Seven Avatar asked Nov 16 '11 18:11

B Seven


3 Answers

It turns out that it does not seem possible to do this.

like image 25
B Seven Avatar answered Sep 30 '22 18:09

B Seven


In Rails 3.2.3 the following seems to work for me:

# Hit the root_path to start our session
get root_path

# A helper method to set our session
login_as(@user_fixture)

# Now we can access the session variable
assert session[:user_id]

Not sure if this will work for you, so goodluck!

like image 128
Emerson Avatar answered Sep 30 '22 19:09

Emerson


Just use session[:user] without open_session block

like image 27
NARKOZ Avatar answered Sep 30 '22 18:09

NARKOZ