问题
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.
回答1:
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!
回答2:
Just use session[:user]
without open_session
block
回答3:
It turns out that it does not seem possible to do this.
回答4:
This isn't what integration tests are designed for. You shouldn't modify the session directly, instead you should hit your /logout path.
来源:https://stackoverflow.com/questions/8156559/how-to-access-session-from-rails-integration-test