加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 22:01:37 所属栏目:百科 来源:网络整理
导读:考虑以下集成测试: 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_redi
考虑以下集成测试:
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]

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读