Rails 3.1 中使用 RSpec 进行设计和声明性测试时出现字符串化键错误

发布于 2024-12-11 00:53:45 字数 1373 浏览 0 评论 0原文

让 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

结束

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱要勇敢去追 2024-12-18 00:53:45

你的更新确实帮助我解决了同样的问题。如果我可以稍微扩展您的解决方案,希望这对将 Rails 3.0 升级到 3.1 并使用 Devise 和声明性授权 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) 而不是会话。在 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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文