问题
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