使用 selenium 驱动程序时 authlogic 无法与水豚一起工作

发布于 2024-11-27 19:22:14 字数 469 浏览 1 评论 0原文

我的所有水豚测试都使用默认驱动程序与我的 authlogic 成员区域一起工作,但是当我更改一个测试以使用 selenium 驱动程序(因为其中包含 ajax)时,它会给出我的论文错误:

You must activate the Authlogic::Session::Base.controller with a controller object before creating objects

事情正在使用 authlogic 的默认驱动程序工作,因此必须与硒有关吗?

我已将 Authlogic::TestCase 包含在我的 spec_helper 中以及

activate_authlogic
    domain.user_sessions.create(user)

每个之前的内容中。

请有人帮我解决这个问题吗?

谢谢里克

I have all my capybara tests working with my authlogic members area using the default driver, but when i change one test to use selenium driver as it has ajax in it, it gives my theis error :

You must activate the Authlogic::Session::Base.controller with a controller object before creating objects

Things are working with default driver for authlogic so must be something to do with selenium ??

I have include Authlogic::TestCase in my spec_helper and

activate_authlogic
    domain.user_sessions.create(user)

in a before each.

Any one help me with this please ?

thanks rick

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

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

发布评论

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

评论(3

信愁 2024-12-04 19:22:14

我在这里发布了一个黄瓜解决方案: 通过 authlogic 登录,无需每次都填写表单

对于 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 删除会话,或者直接点击注销链接(如果这在您的规范中更有意义)。

I posted a cucumber solution here: Log-in through authlogic without having to fill in form every time

For RSpec integration tests it's similar.

In your 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

Obviously, if your site is not login only you may want to move the two lines in config.before into a before block in your specific test for logged in specs. If you leave as is you can delete the session with UserSession.find.destroy or obviously follow the logout link (if this makes more sense in your spec).

病女 2024-12-04 19:22:14

我认为以下代码将用于激活 authlogic:

Authlogic::Session::Base.controller = Authlogic::ControllerAdapters::RailsAdapter.new(self)

话虽如此,我更喜欢定义一个实际进入登录表单、填写并登录的步骤。它速度较慢,但​​我很少手动运行整个集成测试套件,通常持续集成服务器会处理这个问题。

I think the following code will work to activate authlogic:

Authlogic::Session::Base.controller = Authlogic::ControllerAdapters::RailsAdapter.new(self)

Having said that, I prefer defining a step that actually goes to the login form, fills it out, and logs in. It's slower, but I rarely run my entire integration test suite manually, usually the continuous integration server takes care of that.

萤火眠眠 2024-12-04 19:22:14

这对我有用(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                                                                                                                                                  

享受!

This work for me (Rails 3.2.1) :

In spec_helper.rb

require 'authlogic/test_case' 
include Authlogic::TestCase

In In my 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                                                                                                                                                  

Enjoy!

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