【问题标题】:authlogic not working with capybara when using the selenium driver使用 selenium 驱动程序时,authlogic 无法与 capybara 一起使用
【发布时间】:2011-08-04 13:46:09
【问题描述】:

我的所有 capybara 测试都使用默认驱动程序与我的 authlogic 成员区域一起工作,但是当我将一个测试更改为使用 selenium 驱动程序时,因为它有 ajax,它给出了我的 theis 错误:

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

事情正在使用 authlogic 的默认驱动程序,所以一定与 selenium 有关??

我在我的 spec_helper 中包含了 Authlogic::TestCase 和

activate_authlogic
    domain.user_sessions.create(user)

在每个之前。

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

谢谢瑞克

【问题讨论】:

    标签: ruby-on-rails selenium rspec authlogic capybara


    【解决方案1】:

    我在这里发布了一个黄瓜解决方案: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 删除会话,或者显然按照注销链接(如果这在您的规范中更有意义)。

    【讨论】:

      【解决方案2】:

      我认为以下代码可以激活 authlogic:

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

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

      【讨论】:

      • 是的。您肯定希望您的集成规范实际使用登录表单。哦,放弃集成规范——改用 Cucumber;好多了。
      • Cucumber 仅在您的客户正在编写或至少阅读功能时才有用(他们当然应该这样做)。否则,您只是添加了另一层。
      • 我不想每次都使用登录表单;仅在实际测试登录表单时。否则,我宁愿不为我的规范增加额外的时间来完成我已经知道可行的步骤。
      • @PhilT 不正确。无论您的客户是否正在阅读这些功能(我的没有),Cucumber 都很有用。额外的抽象层有助于防止您在测试 UI 时像程序员一样思考。我见过的每一个 RSpec 集成规范都至少包含一些“像程序员一样思考”,因为如果你用 Ruby 编写它会很诱人。
      • @Wheeyls 不测试你知道工作的东西适合单元测试。但是对于集成测试,point 是端到端地测试整个应用程序。因此,您不应该嘲笑或绕过任何东西(外部服务除外)。
      【解决方案3】:

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

      享受吧!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-01-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多