ruby-on-rails – 如何从Rails集成测试访问会话?

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – 如何从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:未定义的方法“session”为nil:NilClass

如何从Rails中的集成测试修改会话参数?

在Rails 3.0.7中工作,Ruby 1.9.2p180,单元测试框架.

编辑:
get’/ user_page’,nil,{:user => nil}和get(‘/ user_page’,{: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]

不知道这是否适合你,所以好运!

原文链接:https://www.f2er.com/ruby/266597.html

猜你在找的Ruby相关文章