【问题标题】:RSpec+Capybara request specs w/ JS not working带有 JS 的 RSpec+Capybara 请求规范不起作用
【发布时间】:2012-04-16 13:36:24
【问题描述】:

在使用 Javascript 时,我无法让请求规范正常工作。 如果我在没有 Javascript 的情况下运行它们,我的规范 通过(该页面是为使用或不使用 JS 而构建的)。

具体来说,当我执行Post.should have(1).record 之类的断言时,规范会失败。 Capybara 只是不从数据库中获取记录,并且在运行之间没有清理数据库。

我已经尝试在禁用事务装置的情况下使用 DatabaseCleaner - 我猜这是常见的方法。没有骰子。

我也尝试过(并且最好更喜欢)在没有 DatabaseCleaner 的情况下运行,使用事务性固定装置并强制 AR 在线程之间共享相同的连接 (a patch described by José Valim)。同样,没有骰子。

此外,我还尝试在 Capybara-webkit 和 Selenium 之间切换 - 问题仍然存在。

我已经建立了一个示例应用程序,其中只有一个基本的 Post 脚手架,它复制了这个问题:https://github.com/cabgfx/js-specs 有一个带有事务性装置和 AR 共享连接的 spec_helper.rb,还有一个用于其他场景的 spec_helper_database_cleaner.rb。

我通常使用 Spork,但我在两个 spec_helper.rb 文件中都禁用了它,只是为了消除潜在的故障点(在两个应用程序中;“真实”应用程序和示例应用程序)。

我在 Macbook Air 上使用 Pow 进行本地开发,运行 OS X 10.7.3 和 MRI 1.9.3 到 RVM。 (我也在 1.9.2 上试过)。

希望我说得通 - 任何指导/帮助/指针都非常感激!

【问题讨论】:

  • 这在某个时候有效吗?
  • 不使用 Javascript。当我在规范中删除 js: true 时,它​​仍然有效。

标签: ruby-on-rails testing rspec capybara capybara-webkit


【解决方案1】:

Matt - 非常感谢您花时间帮助我! 我尝试使用您的 spec_helper 设置它,使用 Selenium 作为 javascript 驱动程序。

规范仍然失败 - 但我可以看到在 Firefox 中执行的正确行为... 然后我突然意识到,问题可能是因为 Capybara 没有等待 AJAX 请求完成。

然后我恢复到我最初的 spec_helper(使用 Spork 而没有 DatabaseCleaner),并简单地使用 Capybara 的 wait_until { page.has_content? "text I'm inserting with JS" }

我更新了示例应用程序,并在请求规范中添加了sleep 1,因此您可以自己查看。它现在可以在使用和不使用 Spork 的情况下使用,并且 AR 猴子补丁似乎可以完美运行。

【讨论】:

  • wait_until 自 Capybara 2.0 起已被弃用
  • 我相信你可以直接使用find方法,它自然会等到elem存在,如果超时就会爆炸。无需wait_until
【解决方案2】:

我已经使用下面列出的spec_helper.rb 尝试了您的代码,并且测试通过了。请注意,触发数据库清理器的语法与spec_helper_database_cleaner.rb 中的语法略有不同。

我们在生产中使用它,我们也尝试了 Jose Valim 建议的修改,但它对我们不起作用 - 确实如此。

require 'rubygems'
require 'spork'
#uncomment the following line to use spork with the debugger
#require 'spork/ext/ruby-debug'

Spork.prefork do
  # Loading more in this block will cause your tests to run faster. However,
  # if you change any configuration or code from libraries loaded here, you'll
  # need to restart spork for it take effect.

  # This file is copied to spec/ when you run 'rails generate rspec:install'
  ENV["RAILS_ENV"] ||= 'test'
  require File.expand_path("../../config/environment", __FILE__)
  require 'rspec/rails'
  require 'rspec/autorun'

  # Add this to load Capybara integration:
  require 'capybara/rspec'
  require 'capybara/rails'

  include Capybara::DSL

  # 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}

  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

    # 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

    # Include sign_in & sign_out for tests
    # config.include Devise::TestHelpers, :type => :controller

    # Use database_cleaner to ensure a known good test db state as we can't use
    # transactional fixures due to selenium testing
    config.before(:suite) do
      DatabaseCleaner.strategy = :truncation
      DatabaseCleaner.clean_with(:truncation)
    end

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

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

Spork.each_run do
  # This code will be run each time you run your specs.
end

【讨论】:

    【解决方案3】:

    José 的建议对我有用,但在我使用 Spork 时无效。但是将此添加到spec_helper.rb 确实:

    Spork.prefork do
      RSpec.configure do |config|
        # Make it so poltergeist (out of thread) tests can work with transactional fixtures
        # http://www.opinionatedprogrammer.com/2011/02/capybara-and-selenium-with-rspec-and-rails-3/#post-441060846
        ActiveRecord::ConnectionAdapters::ConnectionPool.class_eval do
          def current_connection_id
            Thread.main.object_id
          end
        end
      end
    end
    

    来源:http://www.opinionatedprogrammer.com/2011/02/capybara-and-selenium-with-rspec-and-rails-3/#post-441060846

    【讨论】:

      猜你喜欢
      • 2023-03-13
      • 2013-03-10
      • 1970-01-01
      • 2012-05-28
      • 1970-01-01
      • 1970-01-01
      • 2018-10-16
      • 1970-01-01
      • 2017-04-14
      相关资源
      最近更新 更多