Stringify Keys Error with Devise and Declarative Testing with RSpec in Rails 3.1

安稳与你 提交于 2019-12-11 06:26:17

问题


Having a really tough time getting Devise and Declarative to play nicely in RSpec testing.

https://github.com/stffn/declarative_authorization/issues/95

stringify_keys error after supplementing session variables in functional tests

These both address my problem, but neither had a solution that worked for me.

# practices_controller_spec.rb
it "assigns a new practice as @practice as owner" do
  get_action(:new, @owner)
  assigns(:practice).should be_a_new(Practice)
  sign_out @owner
end

def get_action(action,user,*id)
  sign_in user
  get_with user, action, {:id => id}, session
end

# Spec test yields
Failure/Error: get_with user, action, {:id => id}, session
 NoMethodError:
   private method `stringify_keys' called for #<ActionController::TestSession:0x00000100d8a170>

Session looks like this: {"warden.user.user.key"=>["User", [1840], "$2a$04$2Rq4bHGp.tlIgKHE4PlRle"]}

Any suggestions to resolve this error?

Any help would be greatly appreciated. Thanks!

UPDATE

I got it to work by doing this:

  def get_action(action,user,*id)
  sign_in user
  hashy = session['warden.user.user.key'][2]
  get_with user, action, {:id => id}, {"warden.user.user.key"=>["User", [user.id],hashy]}, nil

end


回答1:


Your update really helped me fix the same problem. If I can extend your solution a little bit in the hope this might be helpful for the (probably now few) people upgrading Rails 3.0 to 3.1 and using the Devise and Declarative Authorization gems.

I'm using Test::Unit rather then RSpec, but I assume this can be easily integrated. I would add the following to ActiveSupport::TestCase (or whatever your testcase class is inheriting from in RSpec). Doing this guarantees that the other session key/value pairs are passed to the request also.

class ActiveSupport::TestCase
  include Authorization::TestHelper # provides the declarative authorization get_with method

  def session_hash(user)
    temp_session = session.dup
    temp_session.delete("warden.user.user.key")
    {"warden.user.user.key"=>["User", [user.id],session['warden.user.user.key'][2]]}.merge(temp_session)
  end
end

And in your method the get_with request then uses session_hash(user) instead of session. In Test::Unit the nil at the end was not necessary

def get_action(action,user,*id)
  sign_in user
  get_with user, action, {:id => id}, session_hash(user)
end

It seems that Declarative Authorization does not like ActionController::TestSession from Rails 3.1



来源:https://stackoverflow.com/questions/7827247/stringify-keys-error-with-devise-and-declarative-testing-with-rspec-in-rails-3-1

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