RSpec + Capybara请求规格w / JS无法正常工作

使用Javascript时,我无法获得请求规范。 如果我在没有Javascript的情况下运行它们,我的规范就会通过 (该页面可以使用或不使用JS)。

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

我已经尝试使用DatabaseCleaner禁用事务管道 – 我猜这是常见的方法。 没有骰子。

我也尝试(并且,理想情况下更喜欢)在没有DatabaseCleaner的情况下运行,使用事务夹具并强制AR在线程之间共享相同的连接( 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进行本地开发,使用MRI 1.9.3至RVM运行OS X 10.7.3。 (我也试过1.9.2)。

希望我有意义 – 任何指导/帮助/指针都非常感谢!

马特 – 非常感谢您花时间帮助我! 我尝试使用您的spec_helper进行设置,使用Selenium作为javascript驱动程序。

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

然后我恢复了我最初的spec_helper(使用Spork而没有DatabaseCleaner),只是使用了wait_until { page.has_content? "text I'm inserting with JS" }wait_until { page.has_content? "text I'm inserting with JS" } wait_until { page.has_content? "text I'm inserting with JS" }

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

我已经使用下面列出的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 

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