我的所有capybara测试都使用默认驱动程序与我的authlogic成员区域一起工作,但是当我更改一个测试以使用selenium驱动程序,因为它有ajax,它会给我的错误:
You must activate the Authlogic::Session::Base.controller with a controller object before creating objects
使用authlogic的默认驱动程序,所以必须与selenium ??
有关我在spec_helper和
中包含了Authlogic :: TestCaseactivate_authlogic
domain.user_sessions.create(user)
在每个之前。
有人帮我这个吗?
谢谢瑞克
答案 0 :(得分:5)
我在这里发布了黄瓜解决方案:Log-in through authlogic without having to fill in form every time
对于RSpec集成测试,它是类似的。
在spec_helper.rb
:
require "authlogic/test_case"
RSpec.configure do |config|
...
config.include Authlogic::TestCase
ApplicationController.skip_before_filter :activate_authlogic
config.before(:each, :type => :request) do
activate_authlogic
UserSession.create(User.find_by_email!(email))
end
...
end
显然,如果您的站点不是仅登录,您可能需要将config.before中的两行移动到特定测试中的before块中以获取登录的规范。如果您保持原样,可以删除UserSession.find.destroy
的会话,或者明显遵循注销链接(如果这在您的规范中更有意义)。
答案 1 :(得分:3)
我认为以下代码可以激活authlogic:
Authlogic::Session::Base.controller = Authlogic::ControllerAdapters::RailsAdapter.new(self)
话虽如此,我更喜欢定义一个实际进入登录表单,填写并登录的步骤。它速度较慢,但我很少手动运行整个集成测试套件,通常是持续集成服务器负责这一点。
答案 2 :(得分:0)
这项工作对我来说(Rails 3.2.1):
在 spec_helper.rb
中require 'authlogic/test_case'
include Authlogic::TestCase
在在我的controller_specs中:
def valid_session
activate_authlogic # run before tests are executed
user = Factory(:user)
UserSession.create(user, true) #create an authlogic session
@user = @controller.current_user
{}
end
# exemple of valid_session utilization in your test:
# valid_session
# user_id = @user.id
#
# or
#
# get :index, {}, valid_session
享受!