1

让 Devise 和 Declarative 在 RSpec 测试中发挥出色非常困难。

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

在功能测试中补充会话变量后出现 stringify_keys 错误

这些都解决了我的问题,但都没有适合我的解决方案。

# 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"]}

有什么建议可以解决这个错误吗?

任何帮助将不胜感激。谢谢!

更新

我通过这样做使它工作:

  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

结尾

4

1 回答 1

0

您的更新确实帮助我解决了同样的问题。如果我可以稍微扩展您的解决方案,希望这可能对(可能现在很少)将 Rails 3.0 升级到 3.1 并使用设计和声明性授权 gem 的人有所帮助。

我使用的是 Test::Unit 而不是 RSpec,但我认为这可以很容易地集成。我会将以下内容添加到 ActiveSupport::TestCase (或者您的测试用例类从 RSpec 中继承的任何内容)。这样做可以保证其他会话键/值对也被传递给请求。

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

然后在您的方法中,get_with 请求使用 session_hash(user) 而不是 session。在 Test::Unit 中,最后的 nil 是不必要的

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

似乎声明式授权不喜欢 Rails 3.1 中的 ActionController::TestSession

于 2013-05-20T16:19:06.133 回答