ruby-on-rails - 从 Selenium 更改为 Capybara-Webkit 后出现 cache_classes/Spork 问题

标签 ruby-on-rails testing rspec webkit spork

有一段时间我在使用 Selenium/Spork/Rspec,将 cache_classes 设置为 false,一切似乎都正常。

在切换到 webkit 时,我开始遇到与 cache_classes 相关的错误(例如预期的用户,得到用户),所以我一直在努力尝试将 cache_classes 设置为 true。

但是无论我做什么,我都会遇到以下错误:

 Capybara::Driver::Webkit::WebkitInvalidResponseError:
   Unable to load URL: http://127.0.0.1:56398/login

我已经尝试过各种方法...包括:

开始怀疑我是否应该接受 cache_classes = false,并弄清楚如何避免 Factory girl 错误。任何帮助,将不胜感激。我的spork文件如下:

require 'spork'

Spork.prefork do
  ENV["RAILS_ENV"] ||= 'test'

  require 'rspec/rails'
  require 'rspec/autorun'
  require 'capybara/rails'
  require 'capybara/rspec'
  require 'database_cleaner'
  require 'factory_girl'
  require 'authlogic/test_case'
  require 'email_spec'
  include Authlogic::TestCase

  require File.expand_path("../../config/environment", __FILE__)

  RSpec.configure do |config|
    # == Mock Framework
    #
    # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
    #
    # config.mock_with :mocha
    # config.mock_with :flexmock
    # config.mock_with :rr
    config.mock_with :rspec

    ApplicationController.skip_before_filter :activate_authlogic

    config.include(EmailSpec::Helpers)
    config.include(EmailSpec::Matchers)
    config.include Capybara::DSL

    # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
    #config.fixture_path = "#{::Rails.root}/spec/fixtures"

    # If you're not using ActiveRecord, or you'd prefer not to run each of your
    # examples within a transaction, remove the following line or assign false
    # instead of true.
    config.use_transactional_fixtures = false

    # If true, the base class of anonymous controllers will be inferred
    # automatically. This will be the default behavior in future versions of
    # rspec-rails.
    config.infer_base_class_for_anonymous_controllers = false

    config.before(:suite) do
      DatabaseCleaner.strategy = :truncation, {:except => %w[ages coefficients weights1 weights2 weights3 weights4 weights5 weights6 weights7 weights8 etfs]}
      DatabaseCleaner.clean
    end

    config.before(:each) do
      DatabaseCleaner.start
      Capybara.current_driver = :webkit if example.metadata[:js]
      #Capybara.current_driver = :selenium if example.metadata[:js]
      activate_authlogic
      ActiveRecord::Base.instantiate_observers
    end

    config.after(:each) do
      DatabaseCleaner.clean
      Capybara.use_default_driver if example.metadata[:js]
    end

    Capybara.default_selector = :css

  end

  # Requires supporting ruby files with custom matchers and macros, etc,
  # in spec/support/ and its subdirectories.
  Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}

  ## SUPPORT METHODS ##
  ## (erased for clarity) ##


  ## ##
  ActiveSupport::Dependencies.clear
end

Spork.each_run do
  #FactoryGirl.reload

  # Required to fix a recurring error when testing Active_Admin stuff
  # See here: http://railsgotchas.wordpress.com/2012/01/31/activeadmin-spork-and-the-infamous-undefined-local-variable-or-method-view_factory/
  # Delete at some point if active admin or whoever fixes this
  ActionView::Template.register_template_handler :arb, lambda { |template|
    "self.class.send :include, Arbre::Builder; @_helpers = self; self.extend ActiveAdmin::ViewHelpers; @__current_dom_element__ = Arbre::Context.new(assigns, self); begin; #{template.source}; end; current_dom_context"
  }

  #ActiveSupport::Dependencies.clear
end

更新:添加示例规范以防万一它有帮助....

describe "Items" do
  before(:each) do
    @user = Factory.create(:user)
    activate_authlogic
    b = # something not important
  end

  describe "usage paths" do

    it "the form directly from the basic_simulation show page should have correctly functioning javascript validation", :js => true do
      request_sign_in(@user) # This is a helper method which goes through the login form
      visit '/basic_simulation'
      fill_in "amount", :with => "-5000"
      click_button "Calculate"
      page.should have_selector("label.jquery-validator.amount-error", :text => "Please enter a value greater than or")
      fill_in "amount", :with => "5000"
      click_button "Calculate"
      page.should have_selector("input#amount", :value => "5000")
    end
end

最佳答案

由于 Capybara-webkit 和测试套件的线程问题,您遇到了问题。

Jose Valim 在 a recent blog post. 中解释得更清楚

如果您遵循他的建议,那么您应该能够打开事务固定装置,完全删除数据库清理器,并且在使用 capybara-webkit 进行测试期间不会再出现数据问题。您也会在测试性能方面得到很好的提升。

技巧是确保 Jose 的建议在 Spork.each_run block 中,否则它将不起作用。为了清楚起见,这里是我的 spec_helper.rb 的相关部分。

require 'spork'

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

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

  RSpec.configure do |config|

    config.mock_with :rspec

    config.fixture_path = "#{::Rails.root}/spec/fixtures"
    config.use_transactional_fixtures = true

    Capybara.default_driver     = :rack_test
    Capybara.default_selector   = :css
    Capybara.javascript_driver  = :webkit
  end
end

Spork.each_run do

  if Spork.using_spork?
    ActiveRecord::Base.instantiate_observers
  end

  require 'factory_girl_rails'

  # Forces all threads to share the same connection, works on Capybara because it starts the web server in a thread.
  class ActiveRecord::Base
    mattr_accessor :shared_connection
    @@shared_connection = nil

    def self.connection
      @@shared_connection || retrieve_connection
    end
  end

  ActiveRecord::Base.shared_connection = ActiveRecord::Base.connection
end

其他一些小建议:

  • 如果您使用的是最新版本的 factory_girl_rails,那么您 应该在 Spork.each_run 中使用 require factory_girl_rails block 和 require factory_girl 应该从 prefork
  • 中移除
  • 最新的 factory_girl_rails 也不再需要 ActiveSupport::Dependencies.clear,尽管有些人在没有它的情况下仍然遇到问题,因此您应该尝试删除它。
  • 我仍然不确定是否需要 ActiveRecord::Base.instantiate_observers 但无论如何,只有在使用观察器时才需要它,我知道它应该在 each_run block 。

尝试所有这些,看看它是否适合你。

关于ruby-on-rails - 从 Selenium 更改为 Capybara-Webkit 后出现 cache_classes/Spork 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9476880/

相关文章:

java - 在 Eclipse 中调试时实时更改变量?

ruby-on-rails - RSpec——对象*应该*创建一次,而是为每个测试创建

ruby-on-rails - 带有初始数据的 docker postgres 不会在提交中持久化

unit-testing - 如何模拟已声明的私有(private)字段?

javascript - 在 Rails 4 应用程序中使用 Themeforest 中的 Canvas 主题

java - 我收到 NoSuchElement 异常错误

ruby-on-rails-3 - Rails 3 集成测试 - 使用 webrat fill_in 找不到字段

ruby - 你如何模拟 RSpec 中的 break 语句?

ruby-on-rails - Rails 和 Github 操作 - 凭证

ruby-on-rails - 一个 Rails 应用程序,多个域名