![](/img/trans.png)
[英]Rspec+capybara request specs: testing form submit with invisible button: best way to make button invisible?
[英]RSpec+Capybara request specs w/ JS not working
使用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進行本地開發,運行OS X 10.7.3,MRI 1.9.3至RVM。 (我也試過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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.