Ruby on rails 在Rails 3.1中使用design和RSpec声明性测试字符串化密钥错误

Ruby on rails 在Rails 3.1中使用design和RSpec声明性测试字符串化密钥错误,ruby-on-rails,rspec,devise,declarative,stringify,Ruby On Rails,Rspec,Devise,Declarative,Stringify,要在RSpec测试中很好地发挥Desive和Declarative的作用真的很困难 这两种方法都解决了我的问题,但都没有一种对我有效的解决方案 # 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

要在RSpec测试中很好地发挥Desive和Declarative的作用真的很困难

这两种方法都解决了我的问题,但都没有一种对我有效的解决方案

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

结束

您的更新确实帮助我解决了同样的问题。如果我能稍微扩展一下您的解决方案,希望这对(可能现在很少)将Rails 3.0升级到3.1并使用Desive和Declarative Authorization gems的人有所帮助

我使用的是Test::Unit,而不是RSpec,但我认为这可以很容易地集成。 我会将以下内容添加到ActiveSupport::TestCase(或者您的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 request然后使用session_散列(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

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