考虑以下集成测试:
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]
不知道这是否适合你,所以好运!