如何从Rails集成测试中访问会话?
考虑以下集成测试:
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
如何从Rails中的集成测试中修改会话参数?
使用Rails 3.0.7,Ruby 1.9.2p180,unit testing框架。
编辑:
get '/user_page', nil, {:user => nil}
和get ('/user_page', nil, {:user => nil} )
产生错误。
在Rails 3.2.3中,以下似乎对我有用:
# 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]
不确定这是否适合你,祝你好运!
只需使用session[:user]
而不使用open_session
块
事实certificate,似乎不可能这样做。
这不是集成测试的目的。 您不应该直接修改会话,而应该按/ logout路径。
- Rails路由到新的关联模型
- 我的ActiveRecord模型的未定义方法“has_attached_file”
- 使用active-model-serializers错误地为act-as-taggable标签加载JSON
- sass-rails需要链轮2.0.0但是轨道4.1.0需要链轮2.12.1
- Gem :: LoadError:试图在Heroku上使用时 – Rails 4
- NoMethodError(未定义的方法`’为nil:NilClass):使用jquery-datatables-rails
- Form_For新的相关模型
- 将参数传递给Active Admin中的新操作
- 使用ERB打印arrays元素