这是我在使用带有 authlogic 的 @javascript 运行 cucumber 测试时遇到的错误:
You must activate the Authlogic::Session::Base.controller with a controller object before creating objects
这是我在 feature/support/authlogic.rb 中的 authlogic 支持代码:
require "authlogic"
require "authlogic/test_case"
World(Authlogic::TestCase)
ApplicationController.skip_before_filter :activate_authlogic
Before do
activate_authlogic
end
这是我创建 session 的方式:
def create_session
Session.create(:name => "test", :password => "test-33")
end
如果没有@javascript,它不会给我关于 authlogic 未被激活的错误,但是使用@javascript 时它会。我该如何解决这个问题?
最佳答案
Selenium 和 capybara-webkit 在启动进程时使用不同的线程。当您运行 activate_authlogic 时,它会执行以下操作
Authlogic::Session::Base.controller = (@request && Authlogic::TestCase::RailsRequestAdapter.new(@request)) || controller
这最终为 :authlogic_controller 设置了一个线程局部变量。问题是,当您开始在带有 @javascript 标记的场景中使用新线程时,它会丢失。
对我来说,解决方法是像这样对 authlogic 代码进行 monkeypatch
module Authlogic
module Session
module Activation
module ClassMethods
def controller
if !Thread.current[:authlogic_controller]
Thread.current[:authlogic_controller] = Authlogic::TestCase::MockController.new
end
Thread.current[:authlogic_controller]
end
end
end
end
end
这复制了在 acivate_authlogic 中所做的事情。确保只修补测试环境。
关于ruby - Capybara + Cucumber + Selenium Driver 的 Authlogic 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10775472/