如何从Rails集成测试访问会话? [英] How to access session from Rails 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
这会产生错误:undefined method '[]='
而且,更改为sess.session[:user] = nil
也会产生错误:NoMethodError: undefined method 'session' for nil:NilClass
And, changing to sess.session[:user] = nil
generates an error as well: NoMethodError: undefined method 'session' for nil:NilClass
如何在Rails的集成测试中修改会话参数?
How do you modify the session params from an integration test in Rails?
在Rails 3.0.7,Ruby 1.9.2p180,单元测试框架中工作.
Working in Rails 3.0.7, Ruby 1.9.2p180, Unit test framework.
get '/user_page', nil, {:user => nil}
和get ('/user_page', nil, {:user => nil} )
会产生错误.
get '/user_page', nil, {:user => nil}
and get ('/user_page', nil, {:user => nil} )
generate errors.
推荐答案
在Rails 3.2.3中,以下内容似乎对我有用:
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!
这篇关于如何从Rails集成测试访问会话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!