0

我正在使用 Authlogic 并且在升级到 Rails 3.2.12 后很难让我的 Selenium 测试与:js=>true一起使用。相同的测试用于在 Rails 3.1.3 下工作。我正在使用 Spork 运行我的测试。

我正在通过 Selenium 登录 Firefox (19.0.2),填写登录表单,但随后我从 Authlogic 收到权限错误,即标准“不允许您访问此操作”。

我可以看到很多人对此有疑问,但正如我所提到的,只有在从 Rails 3.1.3 升级到 Rails 3.2.12 后,这对我来说才是一个问题。我怀疑问题可能出在我的 spec_helper 文件(如下)中,尤其是在

模块授权逻辑

我从这里得到的块:

Capybara + Cucumber + Selenium Driver 的 Authlogic 不工作

**spec_helper.rb**

require 'rubygems'
require 'spork'
require 'authlogic/test_case' 

include Authlogic::TestCase

Spork.prefork do
  ENV["RAILS_ENV"] ||= 'test'
  require File.expand_path("../../config/environment", __FILE__)

  require 'rspec/rails'

  Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}

  RSpec.configure do |config|

    config.color_enabled = true

    ApplicationController.skip_before_filter :activate_authlogic
    config.before(:each, :type => :request) do
      activate_authlogic
    end

    config.include FactoryGirl::Syntax::Methods

    config.include Capybara::DSL

    config.use_transactional_fixtures = false

    config.before(:suite) do
      DatabaseCleaner.clean_with(:truncation)
    end

    config.before(:each) do
      DatabaseCleaner.strategy = :transaction
    end

    config.before(:each, :js => true) do
      Capybara.current_driver = :selenium

      DatabaseCleaner.strategy = :truncation

      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
    end

    config.before(:each) do
      DatabaseCleaner.start
    end

    config.after(:each) do
      DatabaseCleaner.clean
    end

    config.include(MailerMacros)
    config.before(:each) { reset_email }

    config.mock_with :mocha

    config.infer_base_class_for_anonymous_controllers = false

    config.order = "random"

    config.treat_symbols_as_metadata_keys_with_true_values = true
    config.filter_run :focus => true
    config.run_all_when_everything_filtered = true
  end
end

Spork.each_run do
  FactoryGirl.reload
end

我的理解是 Authlogic 和 Selenium Webdriver 在不同的线程上工作,因此在 spec_helper 文件中需要这个补丁。

在请求规范中,我收到来自 Authlogic 的权限错误。这是有问题的请求规范测试:

# UNIT REQUEST SPEC
require 'spec_helper'

describe "Units" do

  describe "GET /admin/units/new" do
    before(:each) do
      activate_authlogic
      UserSession.create FactoryGirl.create(:admin_user, :email => "foo@bar.com", :password => "password", :password_confirmation => "password")      

      visit root_path
      fill_in "user_session[email]", :with => "foo@bar.com"
      fill_in "user_session[password]", :with => "password" 
      click_button "Sign In"    
    end

    it "displays a pop up dialog after unit is created", :focus, :js => true do
      visit new_admin_unit_path
      fill_in "Title", :with => "Unit Title"
      fill_in "Code", :with => "U-TEST"
      fill_in "Learning Outcome", :with => "Some Learning Outcome"
      fill_in "unit[learning_outcomes_attributes][0][assessment_methods_attributes][0][content]", :with => "Some Assessment Criteria"

      click_button "Save and Publish"      
      page.should have_css('div.ui-dialog')
    end
  end
end

我的 Capybara 测试适用于:

activate_authlogic
UserSession.create FactoryGirl.build(:user)

在 before(:each) 块中,问题仅在使用 :js=>true 时出现

所以,我的问题是:

  • 在 Rails 3.2.x 中,spec_helper 修复(模块 Authlogic ...)是否不再有效。
  • 如果情况确实如此,那么让它发挥作用的“正常”方式是什么?
4

1 回答 1

2

今天刚刚重温了一遍。当我升级 Rails 时,我还将 capybara 升级到了最新版本(2.1.0)。似乎将 capybara 从这个版本 (2.1.0) 降级到版本 2.0.1 似乎可以解决问题。我可以看到 capybara (2.0.1) 使用 xpath (1.0.0) 而 capybara (2.1.0) 使用 xpath (2.0.0) 以及不同版本的 nokogiri gem。我不知道那里是否有链接,但如果有人愿意看一下,这是我的 gem 文件:

source 'http://rubygems.org'

gem 'rails', '3.2.12'

gem 'rack-mini-profiler'        
gem 'authlogic'                 
gem 'rails3-generators'   
gem 'mysql2'  
gem 'declarative_authorization' 
gem 'kaminari'                  
gem "foreigner"                
gem 'validates_timeliness'     
gem "activerecord-import", ">= 0.2.0"
gem 'ezcrypto'                 
gem 'thin'                       
gem 'exception_notification'    
gem 'mail'
gem 'libv8', '~> 3.11.8.0'
gem "therubyracer", :require => 'v8'
gem 'jasmine', :group => [:development, :test]  
gem 'sprockets'
gem 'jquery-rails'
gem 'tinymce-rails'             
gem 'acts_as_list'              
gem 'cocaine'
gem 'rmagick'                   
gem 'carrierwave'               
gem 'remotipart'                
gem 'deep_cloneable', '~> 1.4.0'
gem 'delayed_job_active_record' 
gem 'daemons'
gem 'rb-readline'
gem "jquery-migrate-rails", "~> 1.0.0"
gem 'ransack'
# Gems used only for assets and not required
# in production environments by default.
group :assets do
  gem 'sass-rails', "~> 3.2.3"
  gem 'coffee-rails', "~> 3.2.1"
  gem 'uglifier', '>= 1.0.3'
end

# Bundle gems for the local environment. Make sure to
# put test-only gems in this group so their generators
# and rake tasks are available in development mode:
group :development, :test do
  gem 'cheat'                   
  gem 'ruby-growl'
  gem 'letter_opener'           
  gem 'rspec-rails', "~> 2.0"
  gem 'database_cleaner'        
  gem 'rb-fsevent', '~> 0.9.1'
  gem 'guard-rspec'
  gem 'spork-rails'
  gem 'guard-spork'
  gem 'factory_girl_rails'
  gem 'capybara', '2.0.1'
  gem 'launchy'
  gem 'mocha', :require => false
  gem 'better_errors'
  gem 'binding_of_caller'
  gem 'meta_request'
end
于 2013-05-27T22:02:21.020 回答