我正在尝试在使用 Authlogic 和 ActiveRecord SessionStore 的 Rails 3.1 应用程序上编写一个简单的集成测试,但我碰壁了。
首先,我尝试了一种经典方法:在确保 require "authlogic/test_case"
行已经在我们的 test_helper.rb
中之后,我编写了一个调用的设置方法activate_authlogic
然后使用 UserSession(@user)
创建 session 。
这确实创建了一个 session (由 UserSession.find
证明),但是当对 protected 资源执行 get
请求时,响应将重定向到登录表单和 session 被终止(即 UserSession.find
将返回 nil
)
我也尝试过 POST-ing 电子邮件/密码,但如果我将 session 存储更改回 cookie_store(我从 this comment 中发现的东西),这似乎仅有效。
将 session 存储切换到 CookieStore
只是为了测试是一种选择,但有些测试已经依赖于 ActiveRecord 存储。
有没有办法只为一次测试切换 session 存储?对于这个问题,我还缺少其他解决方案吗?
require 'test_helper'
class ProtectedTest < ActionController::IntegrationTest
def setup
activate_authlogic
@user = Factory(:user, :roles => 'user')
UserSession.create(@user)
end
def test_protected
https!
get '/protected'
assert_response :success
end
end
最佳答案
我通过在包含“test_helper”之前设置 ENV["RAILS_ENV"] 来启动需要不同环境中的授权用户的测试,从而想出一个解决方法。然后我将该环境中的 session_store
更改为 cookie_store。
ENV["RAILS_ENV"] = "test_cs"
require 'test_helper'
end
class ProtectedTest < ActionController::IntegrationTest
# ...
end
关于ruby-on-rails - 在与 Rails 3.1/Authlogic/ActiveRecord session 存储的集成测试中无法获得授权用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8121596/