stringify_keys error after supplementing session variables in functional tests

二次信任 提交于 2019-12-08 04:00:26

问题


I'm in the process of upgrading to Rails 3.1.0 from 3.0.10, and I'm using Devise for authentication.

I have a number of functional tests that require me to set a single session variable. Stuff along the lines of:

test "new clears current_lesson_id from session" do
  get :new, nil, {'current_lesson_id' => '234234'}
  assert_response :success
  assert_nil session[:current_lesson_id]
end

This was failing as my session info was clobbering the devise authentication session data, so my previous solution was to merge with the default session:

test "new clears current_lesson_id from session" do
  get :new, nil, authenticated_sesion_with({'current_lesson_id' => '234234'})
  assert_response :success
  assert_nil session[:current_lesson_id]
end

def authenticated_session_with(hash)
  session.merge(hash)
end

All of this worked fine with rails 3.0.10 (with warden 1.0.4 and devise 1.4.2), but no longer with rails 3.1.0 (and warden 1.0.5, devise 1.4.4). Now I'm getting the following error:

NoMethodError: private method `stringify_keys' called for <ActionController::TestSession:0x000001060db590>

I gather this is because the 'session' object is an instance of ActionController::TestSession, and in rails 3.1.0 there are a bunch of instance variables that can't and shouldn't be 'stringified'.

How should I properly access the devise user information (preferably dynamically) so I can add 'current_lesson_id', etc.?

Thanks much,


回答1:


try this to fix your error in Rails 3.1

  sign_in user
  hashy = session['warden.user.user.key'][2]
  get :action, nil, {"warden.user.user.key"=>["User", [user.id],hashy]}, nil

Worked for me. Seems to not like the new way Rails handles TestSessions.




回答2:


I think you better do:

def valid_session
    my_session_values = {:some_key => :some_value}
    session.to_hash.merge my_session_values
end


来源:https://stackoverflow.com/questions/7351405/stringify-keys-error-after-supplementing-session-variables-in-functional-tests

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!